]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Fix ftos_decimals returning -0 instead of 0 in some case
authorterencehill <piuntn@gmail.com>
Mon, 26 Jul 2010 12:33:44 +0000 (14:33 +0200)
committerterencehill <piuntn@gmail.com>
Mon, 26 Jul 2010 12:33:44 +0000 (14:33 +0200)
e.g.: ftos_decimals(-0.00001, 2)  returned  -0.00

qcsrc/common/util.qc

index 08eaaeee74ab4dec57ccf070cba0d826fd900c1b..7639c8df24ecd368b1e47fe7e6a92c70e3dd125e 100644 (file)
@@ -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;
 }