Currency values

BlitzPlus Forums/BlitzPlus Programming/Currency values

bluemoon(Posted 2013) [#1]
Is there anyway to output currency values too 2 decimal places without using jiggery pokery.


xlsior(Posted 2013) [#2]
The easiest way is to use an integer for the amount, and count it in cents instead of dollar/euro/pounds/whatever - e.g. $12.34 becomes 1234

Easy to do math with (automatic rounding), just insert a decimal point two from the right during your *printing* routines instead.


xlsior(Posted 2013) [#3]
Here's a quick and dirty example:


; Add $1.03, $1.21 And $11.47

buy1=103
buy2=121
buy3=1147
a=buy1+buy2+buy3

; NOTE: The printamount function currently only works properly when passing it a value larger than 0.
;       you'll have to come up with something of you own to deal with negative numbers.

Print "You spend $"+printamount$(a)
WaitKey()


Function PrintAmount$(money%)
	tempvar$=money%
	If Len(tempvar$)=1 Then tempvar$="00"+tempvar$
	If Len(tempvar$)=2 Then tempvar$="0"+tempvar$
	total$=Left(tempvar$,Len(tempvar$)-2)+"."+Right$(tempvar$,2)
	Return total$
End Function



bluemoon(Posted 2013) [#4]
thanks xlsior.


bluemoon(Posted 2013) [#5]
Anyone find a bug. Returns a value (to 2 dp). Dont need neg numbers, must have dec point in value.

	DebugLog money$(.999)
	
	While Not KeyHit(1)
	Wend
	
	End
	
	
	Function money$(n#)
	
		s$=Str$(n#)
		
		If Len(s$)-Instr(s$,".",1)=1 Then
			s$=s$+"0"
			Return s$ 
		End If
		
		If Len(s$)-Instr(s$,".",1)=2 Then
			Return s$ 
		End If
		
		
		k$=Left$(s$,Instr(s$,".",1)+2)
		
		If Right$(k$,1) >= 5 Then
			n#=n#+0.01
			s$=Str$(n#)
			DebugLog Str$(n#)
		End If
		
		Return Left$(s$,Instr(s$,".",1)+2)
		
	End Function



bluemoon(Posted 2013) [#6]
There are loads of holes. Why does the str$(26745.339654) return 26745.3 ?. Surely it can deal with bigger numbers than this.Xlsiors method is better


Floyd(Posted 2013) [#7]
Surely it can deal with bigger numbers than this.

Not really.

Floating point numbers are single precision, about seven digits. The Str() function rounds to six digits. It could have been designed to do seven digits, but that's as far as it could reasonably go.

Keep in mind that Blitz was designed for game programming. You can do other things, but have to work around many limitations. As you have seen there is no formatted output. And even if you create your own there is no way to send any output to a printer.


bluemoon(Posted 2013) [#8]
ok, thanks Floyd