[FIX] amount precision bugs

- leading 0's for fraction part disappearing
- first decimal lower than 5 wouldn't result in dropping the decimal
This commit is contained in:
Geert Weening
2014-10-21 13:22:37 -07:00
parent 8b10325895
commit 4be209e286
2 changed files with 43 additions and 2 deletions

View File

@@ -1109,13 +1109,24 @@ Amount.prototype.to_human = function(opts) {
fraction_part = fraction_part.replace(/0*$/, '');
if (fraction_part.length || !opts.skip_empty_fraction) {
// Enforce the maximum number of decimal digits (precision)
if (typeof opts.precision === 'number') {
if (opts.precision <= 0 && fraction_part.charCodeAt(0) >= 53) {
int_part = (Number(int_part) + 1).toString();
if (opts.precision <= 0) {
// increment the int_part if the first decimal is 5 or higher
if (fraction_part.charCodeAt(0) >= 53) {
int_part = (Number(int_part) + 1).toString();
}
fraction_part = '';
} else {
fraction_part = Math.round(fraction_part / Math.pow(10, fraction_part.length - opts.precision)).toString();
// because the division above will cut off the leading 0's we have to add them back again
// XXX look for a more elegant alternative
while (fraction_part.length < opts.precision) {
fraction_part = '0' + fraction_part;
}
}
}