Converting string to float

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1075
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Converting string to float

Post 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!
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Converting string to float

Post by infratec »

Code: Select all

t$="0.60"
Debug StrF(ValF(t$), 2)
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Converting string to float

Post 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)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
tastyraspberry
User
User
Posts: 39
Joined: Fri Sep 28, 2012 10:22 am

Re: Converting string to float

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Converting string to float

Post by IdeasVacuum »

Also, you can use Double instead of Float (unless using an API function that requires float)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 642
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Converting string to float

Post 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.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: Converting string to float

Post 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$
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
marcoagpinto
Addict
Addict
Posts: 1075
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Converting string to float

Post by marcoagpinto »

Thanks, guys!
8)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Converting string to float

Post 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]
DE AA EB
Post Reply