-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #906 from recurly/discount-rounding
fix: discount rounding half up with half cents
- Loading branch information
Showing
7 changed files
with
74 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.
13 changes: 13 additions & 0 deletions
13
packages/public-api-fixture-server/fixtures/coupons/coop-pct-50.json
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"code": "coop-pct-50", | ||
"name": "Test coupon: 50% off", | ||
"discount": { | ||
"rate": 0.50 | ||
}, | ||
"plans": [], | ||
"single_use": false, | ||
"applies_to_non_plan_charges": true, | ||
"applies_to_plans": true, | ||
"applies_to_all_plans": true, | ||
"redemption_resource": "account" | ||
} |
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