Formating floating point/double for display

BlitzMax Forums/BlitzMax Programming/Formating floating point/double for display

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here was the messy and non-flexible code I wrote:

	Method GetMultiplierStrings(M: Double,part1$ Var, part2$ Var)
		'Convert the multiplier number into a string.
		'First get the part before the decimal place.
		Local temp$ = String(M)		
		Part1$ = ccFirstStringToSubChop(temp,".")
		'Now make sure the number after the decimal is a single digit.
		'We need to do this because 1.4 in Double is stored as 1.399999 etc !		
		'We do this by multiplying by 10 and turning into an int.
		Local TempPart2 = Floor(0.05+(Float("0."+temp)*10)) 'round down! (add on a little bit of 0.05 to make sure that .999 numbers go over to the next whole number.)

		'Has it rounded up to the next number?
		If String(TempPart2) = "10" Then
			TempPart2 = 0
			Part1 = String(Part1.ToInt()+1)
		EndIf
		Part2 = String(TempPart2)
	End Method

Function ccFirstStringToSubChop$(s$ Var, sub$)
	'Pass in a String, this will only return the first part up to,
	'but not including, the substring (or end).
	'The source string will have the first part and the substring removed.	
	Local pos% = Instr(s$, sub$)
	'If pos = 0 then the end of the string was reached, so return the whole thing.
	If pos = 0 Then
		Local ret$ = s$
		'now clear s$
		s$ = ""
		Return ret$
	Else
		Local ret$ = Mid(s$, 1, pos-1)
		s$ = Mid$(s$, pos+1, Len(s$)) 'leave remainer in s$
		Return ret$		
	EndIf
End Function