You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'convert a double to a string
'double is rounded to the requested amount of digits after comma
Function NumberToString:String(number:Double, digitsAfterDecimalPoint:Int = 2, truncateZeros:Int = False)
Local pow:Int = 10
For Local i:Int = 1 Until digitsAfterDecimalPoint
pow :* 10
Next
'slower than the loop!
'local pow:int = int(10 ^ digitsAfterDecimalPoint)
'bring all decimals in front of the dot, add 0.5 to "round"
'divide "back" the rounded value
Local tmp:Double = (number * pow + Sgn(number) * 0.5) / pow
'find dot - and keep "digitsAfterDecimalPoint" numbers afterwards
Local dotPos:Int = String(Long(tmp)).length '+1
If tmp < 0 Then dotPos :+ 1
Local s:String = String(tmp)[.. dotPos + 1 + digitsAfterDecimalPoint]
's = _StrLeft(string(tmp), dotPos + 1 + digitsAfterDecimalPoint)
'remove 0s? 1.23000 => 1.23, 1.00 = 1
If truncateZeros
While s<>"" And _StrRight(s, 1) = "0"
s = s[.. s.length-1]
Wend
'only "xx." left?
If _StrRight(s, 1) = "." Then s = s[.. s.length-1]
EndIf
Return s
End Function
Good day,
I'd be nice to be able to round floats/doubles to a certain decimal place and also convert floats/doubles to a "nice" looking string.
I've come up with a solution but surely there is a better way, and I don't know if my solution works on other platforms.
Thanks!
The text was updated successfully, but these errors were encountered: