From: terencehill Date: Wed, 30 Mar 2016 12:23:25 +0000 (+0200) Subject: Reimplement ftos_mindecimals in a simpler and more correct way X-Git-Tag: xonotic-v0.8.2~970^2~2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=3654de311fe042687b432afccfd0f307eccdac7f;p=xonotic%2Fxonotic-data.pk3dir.git Reimplement ftos_mindecimals in a simpler and more correct way --- diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index 380afb9ee..384188091 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -286,22 +286,17 @@ string ftos_decimals(float number, int decimals) { // inhibit stupid negative zero if (number == 0) number = 0; - // we have sprintf... return sprintf("%.*f", decimals, number); } -// strips trailing 0s from a float number, e.g. 1.200000 -> 1.2 +/** + * converts a number to a string with the minimum number of decimals + */ string ftos_mindecimals(float number) { - string s = ftos(number); - int k = strstrofs(s, ".", 0); - if(k >= 0) - { - int i = strlen(s); - while(substring(s, i - 1, 1) == "0") i--; - s = substring(s, 0, i); - } - return s; + // inhibit stupid negative zero + if (number == 0) number = 0; + return sprintf("%.7g", number); } int vercmp_recursive(string v1, string v2)