Announcement

Collapse
No announcement yet.

Number output formatting

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Number output formatting

    Is it possible to format a number for display such that it will place commas properly. I've found all the obvious formatting options but none of them seem to do this.

    e.g. I want 15567.00 to display as 15,567.00.

    Do I just need to write a quick function to do it or is it already there in Java or EFS?

  • #2
    ebr
    I am not aware of an existing function or formatting option that would do what you want so you will need to write your own.
    Alex

    Comment


    • #3
      Okay, Thanks, Alex. I went ahead and wrote one that covers my limited needs.

      Comment


      • #4
        ebr
        You are most welcome.
        I ran some searches on the net for some JavaScript functions to format numbers and came across this one that you [or someone else] may find useful. Comments on its use are included at the end of the script
        Alex

        PHP Code:
        function main(){
            var 
        mynum close(0);
            
        mynum formatNumber(mynum,2,',','.','','','-','');//see Notes below
            
        debugPrintln(mynum);
            
        drawText(mynum,TopRow1,Color.blue,Text.BOLD,"mynum");
        }
         
        function 
        formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
            var 
        Math.round(num Math.pow(10,dec));
            if (
        >= 0n1=n2='';
            var 
        = (''+Math.abs(x)).split('');
            var 
        y.length dec
            if (
        z<0z--; 
            for(var 
        z0i++) y.unshift('0');
            
        y.splice(z0pnt); 
            while (
        3) {
                
        z-=3y.splice(z,0,thou);
            }
            var 
        curr1+n1+y.join('')+n2+curr2;
            return 
        r;
        }
         
        /*
        Notes

        The first parameter is the number to be formatted
         
        The second parameter is the number of decimal places that the number should have. 
        If the number contains more decimal places than required it will be rounded to the 
        nearest number with that number of decimal places. 
        If it has fewer decimal places than specified zeroes will be added to the end. 
         
        The third parameter is the thousands separator. In this example a comma is used. 
         
        The fourth parameter is the decimal point. Either a period or comma is normal here. 
         
        The fifth and sixth parameters are used for monetary values and one or other of 
        them will contain the currency symbol when required. 
        If the locale uses a currency symbol hard against the left of numbers then 
        place that symbol by itself in the fifth parameter eg. '$'. 
        If one normally has a space after the currency symbol then add it after the symbol
        in this way '$ '. 
        If the currency symbol comes after the amount instead of before then place it in the
        sixth parameter instead of the fifth parameter. 
         
        The seventh and eighth parameters define the characters to place around the number when 
        the value is negative. 
        The usual values for these parameters would be '-','' but there may be a situation where
        one wants to use '(',')' or other character. 
        */ 

        Comment

        Working...
        X