Git Product home page Git Product logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024

Original comment by [email protected] on 13 Apr 2011 at 3:33

from jquery-numberformatter.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.