StrD and Rounding

Just starting out? Need help? Post your questions and find answers here.
Olli
Addict
Addict
Posts: 1196
Joined: Wed May 27, 2020 12:26 pm

Re: StrD and Rounding

Post by Olli »

We should do a best of all the times we break the ghoul in this floating binary format...
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: StrD and Rounding

Post by Tenaja »

You could multiply by 10, or 100, or 10000 it whatever precision you want, then do your rounding. Then divide back, if you want decimals (or do it in the string).
Cyllceaux
Enthusiast
Enthusiast
Posts: 510
Joined: Mon Jun 23, 2014 1:18 pm

Re: StrD and Rounding

Post by Cyllceaux »

My solution. Not good, but worked:

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
Thx @all
Post Reply