From 4759a4ba99a960620668ca579535367dbb739a5e Mon Sep 17 00:00:00 2001 From: terencehill Date: Mon, 26 Jul 2010 14:33:44 +0200 Subject: [PATCH] Fix ftos_decimals returning -0 instead of 0 in some case e.g.: ftos_decimals(-0.00001, 2) returned -0.00 --- qcsrc/common/util.qc | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc index 08eaaeee7..7639c8df2 100644 --- a/qcsrc/common/util.qc +++ b/qcsrc/common/util.qc @@ -228,16 +228,25 @@ string ftos_decimals(float number, float decimals) { string result; string tmp; - float len; - - // if negative, cut off the sign first - if(number < 0) - return strcat("-", ftos_decimals(-number, decimals)); - // it now is always positive! + float len, isNegative; // 3.516 -> 352 number = floor(number * pow(10, decimals) + 0.5); + if(number == 0) + { + if(decimals == 0) + return "0"; + return strcat("0.", substring("0000000000", 0, decimals)); + } + + if(number < 0) + { + isNegative = TRUE; + number = -number; + } + // it now is always positive! + // 352 -> "352" result = ftos(number); len = strlen(result); @@ -262,6 +271,8 @@ string ftos_decimals(float number, float decimals) // and now... INSERT THE POINT! tmp = substring(result, len - decimals, decimals); result = strcat(substring(result, 0, len - decimals), ".", tmp); + if (isNegative) + return strcat("-", result); // restore the sign return result; } -- 2.39.2