]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
Remove trailing 0s before saving float numbers into cvars in the menu. Reason: with...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index 47a8175cac598554b3f2965a7dfc83db5429d2f5..380afb9eeccdbf8a52818efdb5ae29e71fa904f3 100644 (file)
@@ -281,7 +281,6 @@ void buf_save(float buf, string pFilename)
 
 /**
  * converts a number to a string with the indicated number of decimals
- * works for up to 10 decimals!
  */
 string ftos_decimals(float number, int decimals)
 {
@@ -291,6 +290,20 @@ string ftos_decimals(float number, int decimals)
        return sprintf("%.*f", decimals, number);
 }
 
+// strips trailing 0s from a float number, e.g. 1.200000 -> 1.2
+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;
+}
+
 int vercmp_recursive(string v1, string v2)
 {
        int dot1 = strstrofs(v1, ".", 0);