Just want 1 number behind the decimal point

Blitz3D Forums/Blitz3D Beginners Area/Just want 1 number behind the decimal point

jigga619(Posted 2009) [#1]
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


Spike314(Posted 2009) [#2]
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



Beaker(Posted 2009) [#3]
Or this:
Function Format$(num$, places%=1)
	Return Left(num,Instr(num,".")+places)
End Function
(converted from BlitzMax)


Warner(Posted 2009) [#4]
x# = rnd(0, 1)
Print Int(x# * 10) / 10.0



GfK(Posted 2009) [#5]
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.


jfk EO-11110(Posted 2009) [#6]
When it's got to be fast then better use something like the example by warner, because strings are slow compared to ints.


_PJ_(Posted 2009) [#7]
Left$(Fl#,Len(Str(Int(Fl#))+1))