Page 1 of 1

Converting string to float

Posted: Mon Oct 31, 2016 5:47 pm
by marcoagpinto
Hello!

Code: Select all

t$="0.60"
Debug ValF(t$)
Instead of 0.60 it says 0.60000002384186.

How do I get 0.60?

Thanks!

Re: Converting string to float

Posted: Mon Oct 31, 2016 6:14 pm
by infratec

Code: Select all

t$="0.60"
Debug StrF(ValF(t$), 2)

Re: Converting string to float

Posted: Mon Oct 31, 2016 6:17 pm
by TI-994A
marcoagpinto wrote:How do I get 0.60?!

Code: Select all

t$ = "0.60"
t.f = ValF(t$)
Debug StrF(t, 2)

Re: Converting string to float

Posted: Mon Oct 31, 2016 7:35 pm
by tastyraspberry
It doesn't convert exactly because some decimal numbers can't be represented in binary exactly eg 0.1 expands to 0.000110011001100110011 recurring..

http://www.exploringbinary.com/why-0-po ... ing-point/

I presume as 0.6 is 0.1 * 6 it is why you get the 0.60000002384186. it is the closest that can be done in binary.

It has big implications for financial software and you should always represent money as integers of the smallest division ie pence, cent etc and then just divide by 100 to represent the pound/euro/dollar.

Michael

Re: Converting string to float

Posted: Tue Nov 01, 2016 12:13 am
by IdeasVacuum
Also, you can use Double instead of Float (unless using an API function that requires float)

Re: Converting string to float

Posted: Tue Nov 01, 2016 9:47 am
by captain_skank
tastyraspberry wrote:
It has big implications for financial software and you should always represent money as integers of the smallest division ie pence, cent etc and then just divide by 100 to represent the pound/euro/dollar.

Michael
This is true, but I've had trouble in the past displaying the result becasue you have to convert back to a string to do so ( using StrF or StrD ) and would love for PB to have currency handling functions built in.

Re: Converting string to float

Posted: Wed Nov 02, 2016 2:00 am
by citystate
captain_skank wrote:
tastyraspberry wrote:
It has big implications for financial software and you should always represent money as integers of the smallest division ie pence, cent etc and then just divide by 100 to represent the pound/euro/dollar.

Michael
This is true, but I've had trouble in the past displaying the result becasue you have to convert back to a string to do so ( using StrF or StrD ) and would love for PB to have currency handling functions built in.
for decimal currencies (ie Pounds, Euros, Dollars), perhaps something like this to displaying the integer?

Code: Select all

currency.l = 12345 ; $123.45
output$ = Str(currency/100) + "." + Str(currency%100)
debug output$

Re: Converting string to float

Posted: Wed Nov 02, 2016 2:28 am
by marcoagpinto
Thanks, guys!
8)

Re: Converting string to float

Posted: Wed Nov 02, 2016 9:08 am
by davido
@citystate,

May I suggest the slight modification to take care of less than 10 cents.

Code: Select all

currency.l = 12305 ; $123.05
output$ = Str(currency/100) + "." + RSet(Str(currency%100),2,"0")
Debug output$[/quote]