1. #1
    Sencha User
    Join Date
    Oct 2009
    Posts
    95
    Answers
    3
    Vote Rating
    1
    Albareto is on a distinguished road

      0  

    Default Unanswered: How to format numbers?

    Unanswered: How to format numbers?


    Hi there,

    In ExtJS there is a very nice function Ext.util.Format.number that formats numbers (obvious), I haven't seen it in Sencha Touch and also couldn't find any useful information in the internet. What's the correct way of formatting a number? I need to format it depending on user's localization but it seems not to be so easy as I expected.

    Thanks in advance

  2. #2
    Sencha User
    Join Date
    Oct 2009
    Posts
    95
    Answers
    3
    Vote Rating
    1
    Albareto is on a distinguished road

      0  

    Default


    So nobody knows how to format numbers?

  3. #3
    Sencha Premium Member
    Join Date
    Feb 2012
    Location
    Berne, Switzerland
    Posts
    714
    Answers
    41
    Vote Rating
    42
    ingo.hefti has a spectacular aura about ingo.hefti has a spectacular aura about ingo.hefti has a spectacular aura about

      0  

    Default


    So far Sencha has implemented formatting numbers only in Ext JS.
    So that leaves you with 2 choices:
    1. you can copy over the necessary code from Ext JS (license...?)
    2. you find another solution such as this one http://appointsolutions.com/2012/09/...uch-and-extjs/

  4. #4
    Sencha User
    Join Date
    Oct 2009
    Posts
    95
    Answers
    3
    Vote Rating
    1
    Albareto is on a distinguished road

      1  

    Default


    Yes, well, at the end I decided to backport the ExtJS code, I have license for both components, anyway the ExtJS code is free and you can check the source code in the docs, so here is the function for anyone that might need it:

    Consider that this is inside an object called myObject, you will have to change it to your own thing in case you want to use this, also the defaults thousandSeparator, decimalSeparator and numberFormat. This is just an example.
    Code:
    myObject :
    {
        thousandSeparator : '.',
        decimalSeparator : ',',
        numberFormat : '0.000,00/i',
    
    
        number : function(v, formatString)
        {
            if (!formatString)
            {
                return v;
            }
            v = Ext.Number.from(v, NaN);
            if (isNaN(v)) {
                return '';
            }
            var comma = myObject.thousandSeparator,
                dec   = myObject.decimalSeparator,
                i18n  = false,
                neg   = v < 0,
                hasComma,
                psplit,
                fnum,
                cnum,
                parr,
                j,
                m,
                n,
                i;
    
    
            v = Math.abs(v);
    
    
            // The "/i" suffix allows caller to use a locale-specific formatting string.
            // Clean the format string by removing all but numerals and the decimal separator.
            // Then split the format string into pre and post decimal segments according to *what* the
            // decimal separator is. If they are specifying "/i", they are using the local convention in the format string.
            if (formatString.substr(formatString.length - 2) == '/i')
            {
                var myI18NFormatCleanRe = I18NFormatCleanRe;
                if (!myI18NFormatCleanRe)
                {
                    myI18NFormatCleanRe = new RegExp('[^\\d\\' + myObject.decimalSeparator + ']','g');
                }
                formatString = formatString.substr(0, formatString.length - 2);
                i18n   = true;
                hasComma = formatString.indexOf(comma) != -1;
                psplit = formatString.replace(myI18NFormatCleanRe, '').split(dec);
            }
            else
            {
                hasComma = formatString.indexOf(',') != -1;
                psplit = formatString.replace(formatCleanRe, '').split('.');
            }
    
    
            if (psplit.length > 2)
            {
                //<debug>
                Ext.Error.raise(
                {
                    sourceClass: "Ext.util.Format",
                    sourceMethod: "number",
                    value: v,
                    formatString: formatString,
                    msg: "Invalid number format, should have no more than 1 decimal"
                });
                //</debug>
            }
            else if (psplit.length > 1)
            {
                v = Ext.Number.toFixed(v, psplit[1].length);
            }
            else
            {
                v = Ext.Number.toFixed(v, 0);
            }
    
    
            fnum = v.toString();
    
    
            psplit = fnum.split('.');
    
    
            if (hasComma)
            {
                cnum = psplit[0];
                parr = [];
                j = cnum.length;
                m = Math.floor(j / 3);
                n = cnum.length % 3 || 3;
    
    
                for (i = 0; i < j; i += n)
                {
                    if (i !== 0) {
                        n = 3;
                    }
    
    
                    parr[parr.length] = cnum.substr(i, n);
                    m -= 1;
                }
                fnum = parr.join(comma);
                if (psplit[1])
                {
                    fnum += dec + psplit[1];
                }
            }
            else
            {
                if (psplit[1])
                {
                    fnum = psplit[0] + dec + psplit[1];
                }
            }
    
    
            if (neg)
            {
                /*
                 * Edge case. If we have a very small negative number it will get rounded to 0,
                 * however the initial check at the top will still report as negative. Replace
                 * everything but 1-9 and check if the string is empty to determine a 0 value.
                 */
                neg = fnum.replace(/[^1-9]/g, '') !== '';
            }
    
    
            return (neg ? '-' : '') + formatString.replace(/[\d,?\.?]+/, fnum);
        }
    }
    You can see the original code for this function here: http://docs.sencha.com/ext-js/4-1/so...-method-number
    Enjoy it!

    BTW, for Sencha guys, is there a good reason not to have included this function in the official library?

  5. #5
    Touch Premium Member BostonMerlin's Avatar
    Join Date
    Aug 2010
    Location
    Boston
    Posts
    522
    Answers
    20
    Vote Rating
    41
    BostonMerlin has a spectacular aura about BostonMerlin has a spectacular aura about BostonMerlin has a spectacular aura about

      0  

    Default


    There are a few posts related to "missing format functions". Replies I've seen from Sencha have been <roll_eyes>'mobile doesnt need it'.</roll_eyes>

  6. #6
    Touch Premium Member suzuki1100nz's Avatar
    Join Date
    Apr 2011
    Location
    New Zealand
    Posts
    447
    Answers
    21
    Vote Rating
    62
    suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough

      0  

    Default


    Raise a feature request for Touch asking for Sencha to do what you have done i.e. port the ExtJS code
    I have raised a few of theses and they have been approved for implementation.

  7. #7
    Sencha User
    Join Date
    Oct 2009
    Posts
    95
    Answers
    3
    Vote Rating
    1
    Albareto is on a distinguished road

      0  

    Default


    Hey BostonMerlin, I read some topics about it but with not a lot of useful information. Well, it seems that we are more than two or three that think this is necessary...

    Suzuki, how can I create a change request, in the Discussion forum or something like that?

    Cheers!

  8. #8
    Touch Premium Member suzuki1100nz's Avatar
    Join Date
    Apr 2011
    Location
    New Zealand
    Posts
    447
    Answers
    21
    Vote Rating
    62
    suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough suzuki1100nz is a jewel in the rough

      0  

    Default


    Touch feature request forum is in the premium forums - I take it you don't have access?
    If you don't I'll post the request and reference this thread

  9. #9
    Sencha User
    Join Date
    Oct 2009
    Posts
    95
    Answers
    3
    Vote Rating
    1
    Albareto is on a distinguished road

      0  

    Default


    I have a premium account, I just never took the time to link it to my forum user. I will do that and request the change.

    Thanks for the info and the offering!