From 3ca1a883f914e2830c1b347d3f6c948da41c8f56 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 15 Jan 2015 18:43:49 +0100 Subject: [PATCH] MINOR: tools: add new round_2dig() function to round integers This function rounds down an integer to the closest value having only 2 significant digits. --- include/common/standard.h | 2 ++ src/standard.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index e9900d54a..427d0d725 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -242,6 +242,8 @@ static inline int hex2i(int c) return c; } +/* rounds down to the closest value having max 2 digits */ +unsigned int round_2dig(unsigned int i); /* * Checks for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an diff --git a/src/standard.c b/src/standard.c index 00e672add..93c44bb3c 100644 --- a/src/standard.c +++ b/src/standard.c @@ -504,6 +504,18 @@ int ishex(char s) return 0; } +/* rounds 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 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