]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add new round_2dig() function to round integers
authorWilly Tarreau <w@1wt.eu>
Thu, 15 Jan 2015 17:43:49 +0000 (18:43 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 15 Jan 2015 18:02:27 +0000 (19:02 +0100)
This function rounds down an integer to the closest value having only
2 significant digits.

include/common/standard.h
src/standard.c

index e9900d54a1935fd342927fe92318ffea04cda96c..427d0d7250100f9b1e58e7cac90c11246a24be4a 100644 (file)
@@ -242,6 +242,8 @@ static inline int hex2i(int c)
        return c;
 }
 
+/* rounds <i> down to the closest value having max 2 digits */
+unsigned int round_2dig(unsigned int i);
 
 /*
  * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
index 00e672add4fee380a6f9f9a6f88f47f0ad88f7b8..93c44bb3ce5dbc11968a73b54823bb87443df014 100644 (file)
@@ -504,6 +504,18 @@ int ishex(char s)
        return 0;
 }
 
+/* rounds <i> down to the closest value having max 2 digits */
+unsigned int round_2dig(unsigned int i)
+{
+       unsigned int mul = 1;
+
+       while (i >= 100) {
+               i /= 10;
+               mul *= 10;
+       }
+       return i * mul;
+}
+
 /*
  * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
  * invalid character is found, a pointer to it is returned. If everything is