PDA

View Full Version : floating point currency



sandyrepope
29-05-2008, 02:09
I'm a little confused about what the floating point currency should do when a variable is dimensioned as currency.

I thought it would provide something like .50 but when I use it all my variable holds is .5 (shouldn't it have like .50 instead?)

If not, then how do I make sure that there are always two digits after the decimal?

I'd like to see the variable printed to the console screen as like .50 instead of .5.

Thanks
Sandy

matthew
29-05-2008, 02:38
Hi Sandy, if you want to make sure that the zero at the end is printed you can use the USING$ (http://www.thinbasic.com/public/products/thinBasic/help/html/using$.htm) Command.

Try the following script. :)




uses "Console"

dim a as currency = 1.50

dim aString as string = "#.##"

console_cls

console_writeline("Currency Test")

console_writeline using$(aString, a)

console_waitkey

Petr Schreiber
29-05-2008, 09:29
Hi,

using$ is very good, as it allows multiple numbers to be formatted.

In special cases when 1 number is enough, you can try out FORMAT$:


uses "Console"

dim a as currency = 1.50

console_writeline("Currency Test")

console_writeline("FORMAT$: "+FORMAT$(a, "#.00"))

console_waitkey


Those zeros mean you want exactly 2 decimal places. In format$, unlike using$, the ## would work as "2 digits when possible, lower number of them also accepted".


Petr

sandyrepope
29-05-2008, 14:13
Thanks. I'll give both a try to see which works best in my script.

Sandy