Just want 1 number behind the decimal point
Blitz3D Forums/Blitz3D Beginners Area/Just want 1 number behind the decimal point
| ||
I created this code trying to just have 1 or 2 numbers behind the decimal point, but after the number 9 is printed, their seems to be 4 or more numbers printed (sometimes) behind the decimal. How do I cut the remaining numbers? SeedRnd (MilliSecs()) Graphics 800,600 price#=15 While Not KeyDown(1) If Rand (100)>70 price#=price#+0.1 Else price#=price#-0.1 EndIf Text 20,20," price is :" + Float( price#) Flip Cls Wend End |
| ||
Hi Try this SeedRnd (MilliSecs()) Graphics 800,600,32,2 price#=15 While Not KeyDown(1) If Rand (100)>70 price#=price#+0.1 Else price#=price#-0.1 EndIf Text 20,20," price is :" + Float( price#)+" "+ConvertNumber(price#) Flip Cls Wend End Function ConvertNumber$(Num$) For A=1 To Len(Num) B$= Mid(Num,A,1) If B$="." Txt$ = Txt$ +Mid(Num,A,3) Return Txt Else Txt$=Txt$+B EndIf Next Return Txt End Function |
| ||
Or this:Function Format$(num$, places%=1) Return Left(num,Instr(num,".")+places) End Function(converted from BlitzMax) |
| ||
x# = rnd(0, 1) Print Int(x# * 10) / 10.0 |
| ||
If you need to accurately represent currency, forget floats - they are not accurate enough for this. You need to use separate ints for dollars and cents if you plan on using any mathematical operations, otherwise just use a string. |
| ||
When it's got to be fast then better use something like the example by warner, because strings are slow compared to ints. |
| ||
Left$(Fl#,Len(Str(Int(Fl#))+1)) |