Created
June 15, 2017 16:59
-
-
Save interdigitize/7ba1dc3cbd57d741c355094d8a668aff to your computer and use it in GitHub Desktop.
Prompt: tackling floating-point imprecision with the CashAmount class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://gist.github.com/tim-hr/c9fb047b96d9d36944cec901b7413201 | |
//GOAL: make a class called CashAmount | |
//input: accepts double values (e.g., 14.72) | |
// Note: you can do this by converting to pennies for all denominations so you are always working with integers, then converting back to a two-decimal float as needed. | |
//How to convert: multipy money by 100 REMEMBER to convert back! | |
class CashAmount { | |
constructor(startingMoney) { | |
this.cash = startingMoney; | |
} | |
totalInPennies() { | |
return Math.trunc(this.cash * 100); | |
} | |
//this name could be improved. to get the result in the test, it is using simple addition, not really doubling anything. | |
addDoubleAmount(moreMoney) { | |
this.cash += moreMoney; | |
return this.toDouble(); | |
} | |
// what should the difference be between IN pennies and AS pennies? Why have both? | |
totalAsPennies() { | |
return this.totalInPennies(); | |
} | |
quantityOfEachDenomination() { | |
//Didn't get to this one. | |
//set to denominations obj | |
//{ | |
// 'hundreds': 9, | |
// 'fifties': 1, | |
// 'twenties': 0, | |
// 'tens': 1, | |
// 'fives': 1, | |
// 'ones': 2, | |
// 'quarters': 3, | |
// 'dimes': 1, | |
// 'nickels': 1, | |
// 'pennies': 3 | |
// } | |
//if the number is divisible by 100, | |
//set the property to the right number of times | |
// subtract from the total | |
// if the number is divisable by 50... | |
} | |
toDouble() { | |
return this.cash.toFixed(2); | |
} | |
toDoubleString() { | |
return this.toDouble().toString() | |
} | |
} | |
//TESTS | |
// const cash = new CashAmount(10.50); | |
// console.log(cash.totalInPennies()); // -> 1050 | |
// console.log(cash.addDoubleAmount(29.33)); | |
// console.log(cash.totalAsPennies()); // -> 3983 | |
// const cash = new CashAmount(967.93); | |
// cash.quantityOfEachDenomination() // -> | |
// { | |
// 'hundreds': 9, | |
// 'fifties': 1, | |
// 'twenties': 0, | |
// 'tens': 1, | |
// 'fives': 1, | |
// 'ones': 2, | |
// 'quarters': 3, | |
// 'dimes': 1, | |
// 'nickels': 1, | |
// 'pennies': 3 | |
// } | |
// const cash = new CashAmount(10.50); | |
// console.log(cash.addDoubleAmount(29.33)); | |
// console.log(cash.toDouble()); // -> 39.83 | |
// const cash = new CashAmount(10.50); | |
// cash.addDoubleAmount(29.33); | |
// console.log(typeof cash.toDoubleString()); // -> '39.83' | |
// const cash = new CashAmount(0.10); | |
// console.log(cash.addDoubleAmount(0.20)); | |
// console.log(cash.totalInPennies()); //=== 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment