How to resolve the algorithm Currency step by step in the Factor programming language
How to resolve the algorithm Currency step by step in the Factor 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 Factor programming language
Source code in the factor programming language
USING: combinators.smart io kernel math math.functions money ;
10 15 ^ 4 * 5+50/100 * ! hamburger subtotal
2 2+86/100 * ! milkshake subtotal
+ ! subtotal
dup DECIMAL: 0.0765 * ! tax
[ + ] preserving ! total
"Total before tax: " write [ money. ] 2dip
"Tax: " write [ money. ] dip
"Total with tax: " write money.
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Java programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the JavaScript programming language
You may also check:How to resolve the algorithm K-means++ clustering step by step in the Fortran programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Ring programming language
You may also check:How to resolve the algorithm Yellowstone sequence step by step in the Action! programming language