How to resolve the algorithm Currency step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Currency step by step in the REXX programming language

Table of Contents

Problem Statement

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

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:

(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 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:

Dollar signs and thousands separators are optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Currency step by step in the REXX programming language

Source code in the rexx programming language

/*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.   */


/*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( commas(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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: procedure;  parse arg _;   n= _'.9';      #= 123456789;       b= verify(n, #, "M")
        e= verify(n, #'0', , verify(n, #"0.", 'M') )  - 4       /* [↓]  commatize number*/
           do j=e  to b  by -3;    _= insert(',', _, j);    end  /*j*/;           return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
show$:  return right( commas( '$'arg(1) ), 27)   /*right─justify and format a number.   */


  

You may also check:How to resolve the algorithm Execute a system command step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the Racket programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm 24 game step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Sidef programming language