Re: StrD and Rounding
Posted: Wed Feb 21, 2024 9:55 pm
We should do a best of all the times we break the ghoul in this floating binary format...
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Define val1.d=ValD("83.23195000000001")
Define val2.d=ValD("83.23195")
Debug StrD(val1,4) ; 83.2320
Debug StrD(val2,4) ; 83.2319
Debug FormatNumber(val1,4) ; 83.2320
Debug FormatNumber(val2,4) ; 83.2319
Debug "--------------------------------------"
Procedure.d RoundToDecimalPlaces(value.d, decimalPlaces)
Protected multiplier.d = Pow(10 , decimalPlaces)
Protected roundedValue.d = Round(value * multiplier,#PB_Round_Nearest) / multiplier
ProcedureReturn roundedValue
EndProcedure
Define val1b.d=RoundToDecimalPlaces(val1, 4)
Define val2b.d=RoundToDecimalPlaces(val2, 4)
Debug StrD(val1b,4) ; 83.2320
Debug StrD(val2b,4) ; 83.2320
Debug FormatNumber(val1b,4) ; 83.2320
Debug FormatNumber(val2b,4) ; 83.2320