locbet / jquery-numberformatter

Automatically exported from code.google.com/p/jquery-numberformatter
0 stars 0 forks source link

"Leading zeros" or Issue #22 is not fixed #35

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Using jquery.numberformatter-1.2.1.js

var ss = '' + $.formatNumber(23, { format: "000" });

I expect "023" but getting "23" instead. 
I need to keep that leading zero.

Original issue reported on code.google.com by peterpan...@gmail.com on 17 Dec 2010 at 4:55

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago

Original comment by apar...@gmail.com on 13 Apr 2011 at 3:33

GoogleCodeExporter commented 8 years ago
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