How to resolve the algorithm Currency step by step in the JavaScript programming language
How to resolve the algorithm Currency step by step in the JavaScript 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 JavaScript programming language
This JavaScript code is used to calculate the total cost of hamburgers, shakes, and associated taxes for a given order.
Here's a detailed breakdown of the code:
-
Importing the 'money-math' library:
const money = require('money-math')
This line imports the 'money-math' library, which provides functions for performing monetary calculations.
-
Defining the initial values:
hamburgers
: The number of hamburgers ordered (400,000,000,000,000)hamburgerPrice
: The price of each hamburger ($5.50)shakes
: The number of shakes ordered (2)shakePrice
: The price of each shake ($2.86)tax
: The tax rate (7.65%)
-
Calculating the total cost of hamburgers and shakes:
hamburgerTotal
: The total cost of the hamburgers (calculated by multiplyinghamburgers
byhamburgerPrice
)shakeTotal
: The total cost of the shakes (calculated by multiplyingshakes
byshakePrice
)
-
Calculating the subtotal:
subTotal
: The sum ofhamburgerTotal
andshakeTotal
-
Calculating the tax amount:
taxTotal
: The amount of tax (calculated by applying thetax
percentage tosubTotal
)
-
Calculating the total cost:
total
: The sum ofsubTotal
andtaxTotal
-
Printing out the results: The code uses
console.log
statements to print out the following information:Hamburger Total
: The total cost of the hamburgersShake Total
: The total cost of the shakesSub Total
: The sum of the hamburger and shake totalsTax
: The amount of taxTotal
: The final total cost of the order
Source code in the javascript programming language
const money = require('money-math')
let hamburgers = 4000000000000000
let hamburgerPrice = 5.50
let shakes = 2
let shakePrice = 2.86
let tax = 7.65
let hamburgerTotal = money.mul(hamburgers.toFixed(0), money.floatToAmount(hamburgerPrice))
let shakeTotal = money.mul(shakes.toFixed(0), money.floatToAmount(shakePrice))
let subTotal = money.add(hamburgerTotal, shakeTotal)
let taxTotal = money.percent(subTotal, tax)
let total = money.add(subTotal, taxTotal)
console.log('Hamburger Total:', hamburgerTotal)
console.log('Shake Total:', shakeTotal)
console.log('Sub Total:', subTotal)
console.log('Tax:', taxTotal)
console.log('Total:', total)
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the C# programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the C programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Erlang programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Ursala programming language
You may also check:How to resolve the algorithm Nth root step by step in the C++ programming language