Comments (4)
I needed this also... I changed this piece of code found in the function
jQuery._formatNumber:
var onePortion = "";
if (!(ones == 0 && onesFormat.substr(-1,1) == '#') || forcedToZero) {
// find how many digits are in the group
var oneText = new String(Math.abs(ones));
var groupLength = 9999;
if (onesFormat.lastIndexOf(",") != -1)
groupLength = onesFormat.length - onesFormat.lastIndexOf(",") - 1;
var groupCount = 0;
for (var i = oneText.length - 1; i > -1; i--) {
onePortion = oneText.charAt(i) + onePortion;
groupCount++;
if (groupCount == groupLength && i != 0) {
onePortion = group + onePortion;
groupCount = 0;
}
}
}
To this:
var onePortion = "";
if (!(ones == 0 && onesFormat.substr(-1,1) == '#') || forcedToZero) {
// find how many digits are in the group
var oneText = new String(Math.abs(ones));
var groupLength = 9999;
if (onesFormat.lastIndexOf(",") != -1)
groupLength = onesFormat.length - onesFormat.lastIndexOf(",") - 1;
var groupCount = 0;
var i = onesFormat.length;
var onesIndex = 0;
var oneFormatChar = "";
var oneChar = "";
while (i > -1) {
oneFormatChar = onesFormat.charAt(i);
if (ones >= Math.pow(10, onesIndex))
{
oneChar = oneText.charAt(oneText.length - onesIndex - 1);
}
else
{
oneChar = "0";
}
switch (oneFormatChar) {
case '0':
onePortion = oneChar + onePortion;
groupCount++;
onesIndex++;
break;
case '#':
if (oneChar != "0" || ones >= Math.pow(10, onesIndex)) {
onePortion = oneChar + onePortion;
groupCount++;
}
onesIndex++;
break;
default:
break;
}
if (groupCount == groupLength && onesIndex <= oneText.length - 1) {
onePortion = group + onePortion;
groupCount = 0;
}
if (onesIndex > oneText.length - 1 || i > 0)
i--;
}
}
Works for the following situations, I haven't tested with other format
sequences:
console.log("1000", $.formatNumber(1000, { format: "0000", locale: "us" }));
console.log("1000.76", $.formatNumber(1000.76, { format: "0000.00", locale:
"us" }));
console.log("1000.76", $.formatNumber(1000.76, { format: "#0000.00", locale:
"us" }));
console.log("1000.76", $.formatNumber(1000.76, { format: "#,##0.00", locale:
"us" }));
console.log("1000", $.formatNumber(1000, { format: "#,##0.00", locale: "us" }));
console.log("1000000", $.formatNumber(1000000, { format: "#,##0.00", locale:
"us" }));
Original comment by [email protected]
on 23 Feb 2011 at 4:19
from jquery-numberformatter.
Does indeed look like a regression of issue 22, seems I have no regression test
for that particular one.
Thanks for the code, will investigate further to verify.
Original comment by [email protected]
on 13 Apr 2011 at 3:33
- Changed state: Accepted
from jquery-numberformatter.
Original comment by [email protected]
on 13 Apr 2011 at 3:33
from jquery-numberformatter.
Think I have this fixed now, have tried your various scenarios also, all seems
to now work as intended and all tests pass. SVN updated with fix.
Original comment by [email protected]
on 25 Apr 2011 at 4:22
- Changed state: Fixed
from jquery-numberformatter.
Related Issues (20)
- numberString.indexOf is not a function HOT 3
- {"format":"#,###","locale":"us"} returns 4,100.00 when calling $.formatNumber - should return 4,100 HOT 2
- Rounding error of negative numbers. HOT 2
- Zero number formats with ,00 HOT 2
- Bug parsing percentages with no decimals HOT 3
- $ using us locale HOT 1
- Allow $ in currency HOT 1
- 1515151515.55 formats to 1,515,151,515.5499999523 instead of ,515,151,515.55 HOT 1
- space as separator HOT 1
- Add locale 'bg' HOT 1
- Feature enhancement: Support for locale 'no' HOT 1
- Allow for square formatting HOT 6
- Can only enter 10-based percentages? HOT 1
- pollutes window object with localeGroup property HOT 2
- Percentages are parsed incorrectly HOT 1
- Plugin does not work with latest version of jhashtable plugin HOT 4
- Fix rounding errors HOT 2
- Number being rounded incorrectly
- I need a python version
- Number of integral digits isn't set properly due to toFixed() method rounding up
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery-numberformatter.