-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: discount rounding half up with half cents
Follow tax rounding rules with discounts and round the half cent up to the nearest cent: `39.495 => 39.50`, not `39.49`.
- Loading branch information
Showing
6 changed files
with
61 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
export function clampToZero (number) { | ||
return number < 0 ? 0 : number; | ||
} | ||
|
||
/** | ||
* Round the second decimal of a number without risk of | ||
* floating point math errors | ||
* | ||
* @param {Number} number | ||
* @return {Number} | ||
*/ | ||
export function round (number, digits = 2) { | ||
const rounded = +((number < 0 ? -1 : 1) * Math.round(Math.abs(number) + 'e+2') + 'e-2'); | ||
return parseFloat(rounded.toFixed(digits)); | ||
} | ||
|
||
/** | ||
* Applies a decimal transform on an object's member | ||
* | ||
* @param {String} prop Property on {this} to transform | ||
* @this {Object} on which to apply decimal transformation | ||
*/ | ||
|
||
export function decimalizeMember (prop) { | ||
if (typeof this[prop] !== 'number') return; | ||
this[prop] = decimalize(this[prop]); | ||
} | ||
|
||
/** | ||
* Applies a decimal transform | ||
* | ||
* @param {Number} number to transform | ||
*/ | ||
|
||
export default function decimalize (number) { | ||
return (Math.round(number * 100) / 100).toFixed(2); | ||
export default function decimalize (number, digits = 2) { | ||
return round(number, digits).toFixed(digits); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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