Restored from previous forum. Originally posted by Franco.
Hi all,
try to convert integer to float but the return value is always integer.
I did:
int.l=612534
float.f=int/100
MessageRequester("FLOAT",Str(float),0)
The result is always 6125 instead of 6125.34.
In some other basic languages there is a command to set the numbers of decimals like:
Decimal(1)
Result=6125.3
Decimal(2)
Result=6125.34
and so on.
Didn't find anything in the help files.
In the helpfile to the mathextras library from Wavemaker there is pointed out that there are some undocumented commands like Sqr, Sin, Cos ...
Maybe there is a command to set the decimals of floats?
Thanks in advance.
Have a nice day...
Franco
floating point numbers
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
I think what's happening here isn't a problem with float, but Str is automatically rounding up/down the floating point result.
If you try
a.f=1.9
MessageRequester("Result",Str(a),0)
you get 2
If a is 1.49, you get 1.
If you try
a.f=1.9
b.f=a*a
MessageRequester("Res",Str(b),0
you get 4, so I think your float calculation is being correctly performed but Str is misleading you.
Don
I think what's happening here isn't a problem with float, but Str is automatically rounding up/down the floating point result.
If you try
a.f=1.9
MessageRequester("Result",Str(a),0)
you get 2
If a is 1.49, you get 1.
If you try
a.f=1.9
b.f=a*a
MessageRequester("Res",Str(b),0
you get 4, so I think your float calculation is being correctly performed but Str is misleading you.
Don
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Franco.
I searched a little bit and found in the helpfile to the mathextras library from Wavemaker there is a undocumented command StrF (in the misc2 lib) but this is buggy.
The command from my example:
MessageRequester("FLOAT",StrF(float),0)
will show '6125.339844' instead of '6125.34'.
What now; ...Fred?
Have a nice day...
Franco
That's right, thank you Don for the tip, I appreciate it.I think what's happening here isn't a problem with float, but Str is automatically rounding up/down the floating point result.
I searched a little bit and found in the helpfile to the mathextras library from Wavemaker there is a undocumented command StrF (in the misc2 lib) but this is buggy.
The command from my example:
MessageRequester("FLOAT",StrF(float),0)
will show '6125.339844' instead of '6125.34'.
What now; ...Fred?
Have a nice day...
Franco
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
Franco - yep, a proper StrF function would be very nice. In the meantime, you could use something like this (not as nice as a built in PB function I admit!):
Don
Franco - yep, a proper StrF function would be very nice. In the meantime, you could use something like this (not as nice as a built in PB function I admit!):
Code: Select all
Procedure.s Strfloat(val.f, dp.l)
; val -> float value to convert to string
; dp -> no. of decimal places to round up/down to
r=1 : i=1 : While (i; no [b]x^y[/b] function in PB??
i=i-1
vf.f=val*r
vs.s=Str(vf)
rs.s=Left(vs,Len(vs)-i)+"."+Right(vs,i)
ProcedureReturn rs
EndProcedure
int.l=612534
float.f=int/100
MessageRequester("FLOAT",Strfloat(float,2),0) ; prints "6125.34"
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Franco.
Don, thanks it works fine for me.
I'm translating the mouse coordinates to a value like inch, mil or mm.
And then put the value on the Screen with:
SetGadgetText(0, "X = "+StrFloat(FloatX,4)) ;thanks to Don for StrFloat
SetGadgetText(1, "Y = "+StrFloat(FloatY,4)) ;thanks to Don for StrFloat
It seems to be fast enough for this.
Thanks!
Have a nice day...
Franco
Don, thanks it works fine for me.
I'm translating the mouse coordinates to a value like inch, mil or mm.
And then put the value on the Screen with:
SetGadgetText(0, "X = "+StrFloat(FloatX,4)) ;thanks to Don for StrFloat
SetGadgetText(1, "Y = "+StrFloat(FloatY,4)) ;thanks to Don for StrFloat
It seems to be fast enough for this.
Thanks!
Have a nice day...
Franco
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Don.
Thanks, I forgot about that. That makes the function a bit simpler to read. It becomes:
There's very little difference in speed between this function and the previous one though. Actually this function is very slightly slower!
Don
Thanks, I forgot about that. That makes the function a bit simpler to read. It becomes:
Code: Select all
Procedure.s Strfloat(val.f, dp.l)
r=IPow(10,dp)
vf.f=val*r
vs.s=Str(vf)
rs.s=Left(vs,Len(vs)-dp)+"."+Right(vs,dp)
ProcedureReturn rs
EndProcedure
Don