Format Number Correctly. Use FlipString?

Blitz3D Forums/Blitz3D Programming/Format Number Correctly. Use FlipString?

asdfasdf(Posted 2006) [#1]
I'm trying to make function that format a number correctly
ex.
1000 + "$###,###,###" -> "$1,000"

I'm using this format function

Function Format$(Number,FormatString$)
	MakeFormat$=""
	NumberCount=1
	For I=Len(FormatString$) To 1 Step -1
		GetFormatChar$=Mid$(FormatString$,I,1)
		Select GetFormatChar$
			Case ","
				MakeFormat$=MakeFormat$+","
			Case "."
				MakeFormat$=MakeFormat$+"."
			Case "#"
				If NumberCount > Len(Number) Then
					GetNumberChar$="0"
				Else
					GetNumberChar$=Mid$(Number,NumberCount,1)
				End If
				NumberCount=NumberCount+1
				MakeFormat$=MakeFormat$+GetNumberChar$
		End Select
	Next
	Return MakeFormat$
End Function


With this function:
ex.
1000 + "###,###" -> 100,000

How do I fix this? Would I use a flipString function to flip it and format with this function, then flip the number back? Thanks.


octothorpe(Posted 2006) [#2]
This is one of those rare instances where recursion makes for a much simpler solution.



For an insight into how it works: uncomment the Stop command in the function, check out the Local watch variables, and step forward in the debugger.


asdfasdf(Posted 2006) [#3]
OK thank you for the function. This will work better then having to supply the format string.