USA money to Canadian money

Blitz3D Forums/Blitz3D Beginners Area/USA money to Canadian money

po(Posted 2004) [#1]
I am trying to make a calculator thing that tells you how much Canadian dollars is equivalent to the USA money amount you type in.

It works fine until you type in a decimal number. How could I make it so it calculates money wise and not decimal\integer wise?

Here is what I got:
AppTitle "USA To Canadian Money Calculator"

Global usa_money#

While Not KeyHit(1)

usa_money#=Input("Type in USA money amount: $")

usa_money#=usa_money#*1.25

Print "Canadian equivalent: $"+usa_money#

Wend



xlsior(Posted 2004) [#2]
*** UPDATED ***

NOTE: I noticed a bug in my original message that could give incorrect results due to rounding... Should be fixed in the code below


Here is one way of doing it...

Since there is no "money" kind of variable, you'll have to use a slightly different approach. The easiest way of doing this that I can think of, is sticking with integers -- and instead of counting full and fractional dollars, just count *cents* instead.

You can seperate the full dollars and cents later of course.
(The code below is almost all comments, it's not much longer than your original program)

AppTitle "USA To Canadian Money Calculator"
Global usa_money#
Global canadianmoney

While Not KeyHit(1)

usa_money#=Input("Type in USA money amount: $")

; Multiply the money variable by 100, so now instead of whole and
; fractional dollars, you will be storing cents instead
canadianmoney=usa_money#*1.25*100

; To get the cents, have just the last two digits of the 
; number. Since this is a conventient power of 10, we can 
; just do a MOD 100 and get the remainder that is not 
; divisible by hundred: the cents.
canadiancents$=canadianmoney Mod 100

; For dollars, we can take the same number and subtract the cents. That leaves
; us with just the dollars except the value is 100 times to large.
; divide it by 100 to get the real amount.
canadiandollar$=(canadianmoney-canadiancents$)/100

; Glue the dollars and cents back together, with a period seperating them
Print "Canadian Equivalent: $"+Canadiandollar$+"."+canadiancents

; And just to show...
Print "Canadian Equivalent in Cents: "+canadianmoney

; If you want to do more math, you might need the following two lines:
; canadiandollar=canadiandollar$
; canadiancents=canadiancents$

Wend