Permalink
Browse files

Fixed incorrect fraction formatting in money_format

The function worked incorrectly when the length of the fraction was less than frac_digits.

For instance, for frac_digits=2 the function returned:
money_format("%!i", 31.011); // 31.1
money_format("%!i", 31.01);  // 31.01
money_format("%!i", 31.009); // 31.1
  • Loading branch information...
1 parent a75e2b0 commit 163580b4c908cab44edd69505d4571f0de6c5d65 @evgenius evgenius committed Dec 20, 2012
Showing with 3 additions and 0 deletions.
  1. +3 −0 functions/strings/money_format.js
@@ -121,6 +121,9 @@ function money_format (format, number) {
dec_pt = '';
} else if (right < fraction.length) {
fraction = Math.round(parseFloat(fraction.slice(0, right) + '.' + fraction.substr(right, 1))) + '';
+ if (right > fraction.length) {
+ fraction = new Array(right - fraction.length + 1).join('0') + fraction; // prepend with 0's
+ }
} else if (right > fraction.length) {
fraction += new Array(right - fraction.length + 1).join('0'); // pad with 0's
}

0 comments on commit 163580b

Please sign in to comment.