Closed GoogleCodeExporter closed 8 years ago
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 goo...@helder-solutions.nl
on 23 Feb 2011 at 4:19
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 apar...@gmail.com
on 13 Apr 2011 at 3:33
Original comment by apar...@gmail.com
on 13 Apr 2011 at 3:33
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 apar...@gmail.com
on 25 Apr 2011 at 4:22
Original issue reported on code.google.com by
peterpan...@gmail.com
on 17 Dec 2010 at 4:55