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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Currency step by step in the Quackery 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 Quackery programming language

Source code in the quackery programming language

  [ $ "bigrat.qky" loadfile ] now!

  [ 100 * n->v ]               is dollars      (    n -->  n/d )

  [ n->v v+ ]                  is cents        ( n/d n --> n/d )

  [ rot n->v v* ]              is cost         ( n n/d --> n/d )

  [ $->v drop v* 100 n->v v/ ] is tax          ( n/d $ --> n/d )

  [ 100 n->v v/
    2 point$
    $ "  $" swap join
    ' [ 2 split nip ] ]do[
    dup -3 peek
    char . = if done
    dup -2 peek
    char . = iff
      [ char 0 join ]
      done
    $ ".00" join ]             is currency$    (   n/d --> $   )

    [ currency$ echo$ ]        is echocurrency (   n/d -->     )


  4000000000000000 5 dollars 50 cents cost
                 2 2 dollars 86 cents cost v+

  say "Total price before tax: "  2dup echocurrency cr
 
  2dup $ "7.65" tax
 
  say "Tax:                     " 2dup echocurrency cr
 
  v+
  say "Total price with tax:   "       echocurrency cr

  

You may also check:How to resolve the algorithm Check output device is a terminal step by step in the Perl programming language
You may also check:How to resolve the algorithm OpenWebNet password step by step in the Swift programming language
You may also check:How to resolve the algorithm Filter step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Quine step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Ring programming language