Code archives/User Input/TheCatWalksOnTheKeyboard-proof String-2-Float

This code has been declared by its author to be Public Domain code.

Download source code

TheCatWalksOnTheKeyboard-proof String-2-Float by CS_TBL2005
Handy for scriptparsers and other user input. Instead of bugging the user with error-popups when the input is unreadable, this routine tries to interprete the input and creates a nice new output.

"0.1.2.3" returns "12.3"
"-----5" returns "-5.0"
"1.024-.-5" returns "1024.5"
"1.000.000.00" returns "1000000.0"
"-1- 2 3 4 . 5 - 6" returns "-1234.56"

So, as you see: as a bonus we can now type "10.000.00" or "10 000" if we want tenthousand.

* Space are wiped-out.
* The most-right dot (if any) will be the decimal dot, the rest is ignored.
* The number of minus-chars defines whether it's a positive or negative value.
Function String2Float#(a$)
	;
	;	String2Float, by CS_TBL
	;
	;       allowed in the string are: dot, minus, space, 0123456789
	;	other chars before the 'value' return 0.0, and other things after the 'value' are ignored
	;
	;	examples:
	;
	;	"0.1.2.3" returns "12.3"
	;	"-----5" returns "-5.0"
	;	"1.024-.-5" returns "1024.5"
	;	"1.000.000.00" returns "1000000.0"
	;	"-1- 2 3 4 . 5 - 6" returns "-1234.56"
	;

	; wipe spaces
	a$=Replace$(a$," ","")

	If a$="" Return 0

	l=Len(a$)

	; do we have an odd amount of '-' ?
	az$=Replace$(a$,"-","")
	m=l-Len(az$)

	; yes? it's a negative number!
	If m Mod 2 negative=True

	; scan the value (without - ) for dots
	For t=Len(az$)-1 To 0 Step -1
		If Not found
			If Mid$(az$,t+1,1)="."
				found=True
				foundpos=(Len(az$)-1)-t
			EndIf
		EndIf
	Next
	; so we found the most-right dot, if any..


	; get rid of all the dots then
	az$=Replace$(az$,".","")

	l=Len(az$)

	If found ; place 1 dot back
		az$=Left$(az$,l-foundpos)+"."+Right$(az$,foundpos)
	EndIf

	If negative
		az$="-"+az$
	EndIf

	Return Float(az$)
	
End Function

Comments

None.

Code Archives Forum