ValF of a string problem

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TerryHough.

Could someone open my eyes. This doesn't seem to work as expected.

StringGadget(1, 20, 70,100,20,"") is used to enter a numeric amount with 2 decimal places. Eg. 100000.55

T$=GetGadgetText(1) ; get the string
Amount.f = ValF(T$) ; convert to a float value

Problem is that Amount always contains 100000.546875

Have I run up on a "flaw" or am I doing something wrong.

Thanks,
Terry
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Well, you can't store numbers like xxx.55 in a floating point number easily (or accurately with enough resolution).

Live with it, work with it as strings, or use longwords and parse it manually.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

PB kindly provided me with this :)

Code: Select all

;convert a float to a string and round the float remainder to 'nPlaces' i.e. 103.879997 >> 103.88
Procedure.s floatToString(number.f, nPlaces.l)
    r=Pow(10,nPlaces)
    vf.f=number*r
    vs.s=Str(vf)
    rs.s=Left(vs,Len(vs)-nPlaces)+"."+Right(vs,nPlaces)
    ProcedureReturn rs
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> PB kindly provided me with this [snip]

...which I originally got from Ron -- it wasn't all my work. :)


PB - Registered PureBasic Coder
Post Reply