Task

Show how to represent currency in a simple example, using a data type that represent exact values of dollars and cents.



Note

The IEEE 754 binary floating point representations of numbers like   2.86   and   .0765   are not exact.

For this example, data will be two items with prices in dollars and cents, a quantity for each, and a tax rate.

Use the values:

 4000000000000000 hamburgers at $5.50 each       (four quadrillion burgers)
 2 milkshakes at $2.86 each, and 
 a tax rate of 7.65%. 


(That number of hamburgers is a 4 with 15 zeros after it.   The number is contrived to exclude naïve task solutions using 64 bit floating point types.)

Compute and output (show results on this page):

 the total price before tax
 the tax
 the total with tax 


The tax value must be computed by rounding to the nearest whole cent and this exact value must be added to the total price before tax.

The output must show dollars and cents with a decimal point.

The three results displayed should be:

 22000000000000005.72
 1683000000000000.44 
 23683000000000006.16 


Dollar signs and thousands separators are optional.




/*REXX program shows a method of computing the total price and tax  for purchased items.*/
numeric digits 200                                        /*support for gihugic numbers.*/
taxRate= 7.65                                             /*number is:   nn   or   nn%  */
if right(taxRate, 1)\=='%'  then taxRate= taxRate / 100   /*handle plain tax rate number*/
taxRate= strip(taxRate, , '%')                            /*strip the  %   (if present).*/
item.  =;                          items= 0               /*zero out the register.      */
item.1 = '4000000000000000  $5.50  hamburger'             /*the  first  item purchased. */
item.2 = '               2  $2.86  milkshake'             /* "  second    "      "      */
say  center('quantity', 22)          center("item", 22)           center('price', 22)
hdr= center('',         27, "─")     center('',     20, "─")      center('',      27, "─")
say hdr;                             total= 0
         do j=1  while item.j\==''                        /*calculate the total and tax.*/
         parse var item.j   quantity price thing          /*ring up an item on register.*/
         items    = items + quantity                      /*tally the number of items.  */
         price    = translate(price, , '$')               /*maybe scrub out the $ symbol*/
         subtotal = quantity * price                      /*calculate the     sub-total.*/
         total    = total + subtotal                      /*    "      "  running total.*/
         say right(quantity, 27)     left(thing, 20)        show$(subtotal)
         end   /*j*/
say                                              /*display a blank line for separator.  */
say translate(hdr, '═', "─")                     /*display the separator part of the hdr*/
tax= format(total * taxRate, , 2)                /*round the total tax for all the items*/
say right(items  "(items)", 35)     right('total=', 12)            show$(total)
say right('tax at'  (taxRate * 100 / 1)"%=", 48)                   show$(tax)
say
say right('grand total=', 48)                                   show$(total+tax)
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show$:  return right( '$'arg(1), 27)             /*right─justify and format a number.   */