Format Number Correctly. Use FlipString?

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

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.

This is one of those rare instances where recursion makes for a much simpler solution.

Function format_user_friendly_number$(number$)
	dot_pos = Instr(number$, ".")
	If dot_pos > 0 Then
		left_part$ = Left$(number$, dot_pos-1)
		right_part_including_dot$ = Mid$(number$, dot_pos)
		result$ = format_user_friendly_number$(left_part$) + right_part_including_dot$
		Return result$
	EndIf

	negative_pos = Instr(number$, "-")
	If negative_pos = 1 Then
		left_part$ = "-"
		right_part$ = Mid$(number$, 2)
		result$ = left_part$ + format_user_friendly_number$(right_part$)
		Return result$
	EndIf

	If Len(number$) > 3 Then
		left_part$ = Left$(number$, Len(number$) - 3)
		right_part$ = Right$(number$, 3)
		result$ = format_user_friendly_number$(left_part$) + "," + right_part$
		Return result$
	EndIf

	;Stop
	Return number$
End Function

n = 1234567890
Print n
Print "$" + format_user_friendly_number(n)
WaitKey
End


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.

OK thank you for the function. This will work better then having to supply the format string.