float to string conversion

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 Kale.

this prints "103" in the debug console, is there any way i can convert 103.88 to a string which reads "103.88" without rounding it?

Code: Select all

Debug Str(103.88)
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.

Unlike other BASICs, in PureBasics you use the StrF command when
dealing with floats. So change Str(103.88) to StrF(103.88) to do
it; however, you'll notice that it prints 103.879997 too, so if this
bothers you (until Fred fixes it) then use this procedure instead:

Code: Select all

; Procedure originally by Don.
;
Procedure.s StrFloat(number.f, places.l)
  r=Pow(10,places) : vf.f=number*r : vs.s=Str(vf)
  rs.s=Left(vs,Len(vs)-places)+"."+Right(vs,places)
  ProcedureReturn rs
EndProcedure
;
Debug StrFloat(103.88, 2) ; 2 = # of decimal places.
PB - Registered PureBasic Coder
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.

thanks thats very helpful :) i must of missed StrF in the docs.
Post Reply