From: terencehill Date: Mon, 26 Jul 2010 12:33:44 +0000 (+0200) Subject: Fix ftos_decimals returning -0 instead of 0 in some case X-Git-Tag: xonotic-v0.1.0preview~341^2~13 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=4759a4ba99a960620668ca579535367dbb739a5e;p=xonotic%2Fxonotic-data.pk3dir.git Fix ftos_decimals returning -0 instead of 0 in some case e.g.: ftos_decimals(-0.00001, 2) returned -0.00 --- 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; }