Letting the user enter a float value.

BlitzMax Forums/BlitzMax Beginners Area/Letting the user enter a float value.

Ryan Burnside(Posted 2006) [#1]
I was wondering how i could let a player type in a float value with the keyboard. Nothing fancy just a float var that the player can adjust through typing. Surly there good ways of letting the player type in the value of a float var and dealing with bad results ect.


H&K(Posted 2006) [#2]
Let them input a string. And then parse it yourself


Ryan Burnside(Posted 2006) [#3]
I tried this but It's not what I need.
Strict
Graphics 640,480
Global Str$="_"




While Not KeyDown(KEY_ESCAPE)
Cls
	If GetChar() <> False
		Str:+GetChar()
	EndIf
	SetColor 255,255,255
	DrawText(Str,12,12)
Flip
Wend



klepto2(Posted 2006) [#4]
Try it this way:

Strict
Graphics 640,480
Global Str$=""
Global Point:Byte = False
Global Result:Float = 0




While Not KeyDown(KEY_ESCAPE)
Cls
	Local Ch:Int = GetChar()
	If CH > 47 And ch < 58 Then 
		Str:+Chr(CH)
	EndIf
	If CH = Asc(".") Then
		If Point = False Then
			Str:+Chr(CH)
			Point = True
		EndIf
	EndIf
	If CH = KEY_ENTER Then
		Result = 100.0*Float(Str)
		Str = ""
		Point = False
	EndIf
		
	SetColor 255,255,255
	DrawText(Str+"_",12,12)
	DrawText(Result,12,32)
Flip
Wend


You have to save GetChar once before and the operate only with the saved result, because Getchar deletes the last entered key after execution.


Ryan Burnside(Posted 2006) [#5]
Thanks it's sort of working, why are you multiplying everything by 100? Also if you take out the multiplication by 100 it makes the value slightly off......


klepto2(Posted 2006) [#6]
The multiplication is just an example to show that it works.
You also could simply save the result = String(str) . As said, this was just an example ;)


JazzieB(Posted 2006) [#7]
Float values will pretty much always be slightly off due to how they are stored internally, plus the impossibility to actually store every conceivable value between 0 and 1, as there are an infinite number of values within that range. For example, it is impossible to store EXACTLY 1/3, as the decimal equivalent is 1.33 recurring, forever!

If you require greater accuracy you can use Double's instead, but these too have their limit.

As mentioned, the slight differences are due to how floats/doubles are stored, but I'm no expert on the format of them when stored internally, so maybe someone else can explain it a bit better than I can!