Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

money_format works incorrectly for fractions < 0.1 #61

Merged
merged 2 commits into from

2 participants

@evgenius

Hello,

money_format works incorrectly for fractions < 0.1

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

evgenius added some commits
@evgenius evgenius Removed an extraneous parameter of parseFloat call a75e2b0
@evgenius evgenius 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
163580b
@kvz kvz was assigned
@kvz
Owner

Awesome, thank you!

@kvz kvz merged commit 46e5c8b into kvz:master
@evgenius

You're welcome. Glad I could help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 20, 2012
  1. @evgenius
  2. @evgenius

    Fixed incorrect fraction formatting in money_format

    evgenius authored
    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
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 1 deletion.
  1. +4 −1 functions/strings/money_format.js
View
5 functions/strings/money_format.js
@@ -120,7 +120,10 @@ function money_format (format, number) {
fraction = '';
dec_pt = '';
} else if (right < fraction.length) {
- fraction = Math.round(parseFloat(fraction.slice(0, right) + '.' + fraction.substr(right, 1), 10)) + '';
+ 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
}
Something went wrong with that request. Please try again.