Open thareh opened 1 year ago
This is my Blitzmax-based solution:
'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
Yours looks way more concise.
Hey,
Your solution looks very similar to my Blitzmax-based one I used before! 😄
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!