Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom Formatting Numbers

    jayakodiu@yahoo.com

    Custom Formatting Numbers

    Excel offers customised formatting of numbers; though VBS & VB6 have similar

    functions they work for millions (US system) only; if you you say

    Format(123456789.456, "###,##,##,###.##"), you get 123,456,789.46

    JS snippets for US system are available; the following js function takes any

    type of format that uses comma separaors for integer and zeros for decimals:

    z='###,##,##,###.00'
    var x=123456789.456
    zn=z.length;pk=0;tt=''
    for(i=zn;i>0;i--){if(z.substr(i,1)=='.'){pk=i;break}}
    if(pk==0){dx=x.toFixed(0);cx='';pn=zn}
    if(pk>0){a=z.split('.');dn=a[1].length;tx=x.toFixed(dn)
    tn=tx.length;jk=tn-dn-1;cx=tx.substring(jk);dx=tx.substr(0,jk);pn=pk}
    jk=dx.length-1
    for(i=pn; i>=0; i--){if(jk>=0){zt=z.substr(i,1);if(zt==','){tt=','+tt}
    if(zt=='#'){tt=dx.substr(jk,1)+tt;jk=jk-1;}}}
    alert(tt+cx)


    A simple utility using the above is in the zip file enclosed.

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Custom formatting Numbers
    jayakodiu@yahoo.com

    my post could not attach the zip file; the following is the html file:

    <html>
    <body>Use commas as separators for thousand/million or lacs/crores; zeros for decimals.
    <table border="1" cellpadding="3" cellspacing="3" width="491" style="font-family: Arial; font-size: 14pt" height="105">
    <tr><td align="right" width="59" height="22">Format</td><td width="379" bgcolor="#C0C0C0" height="22">
    <input type=text id=fm value="###,###,###,###,###.000" size="43" style="font-family: Arial; font-size: 14pt" name="fnn"></td></tr>
    <tr><td align="right" width="59" height="25">Number</td>
    <td width="379" height="25"><input type=text value="123456789.9723" size="37" style="border-style: solid; border-width: 1" name="ipt">&nbsp;
    <input type=button value="Ok" onclick=fmt()></td>
    </tr>
    <tr><td align="right" width="59" height="22">Result</td><td width="379" id="rst" height="22"></td></tr>
    </table>
    </body>
    <script language=javascript>
    function fmt(){
    z=fm.value;var x=parseFloat(ipt.value);rst.innerText='';zn=z.length;pk=0;tt=''
    for(i=zn;i>0;i--){if(z.substr(i,1)=='.'){pk=i;break}}
    if(pk==0){dx=x.toFixed(0);cx='';pn=zn}
    if(pk>0){a=z.split('.');dn=a[1].length;tx=x.toFixed(dn)
    tn=tx.length;jk=tn-dn-1;cx=tx.substring(jk);dx=tx.substr(0,jk);pn=pk}
    jk=dx.length-1
    for(i=pn; i>=0; i--){if(jk>=0){zt=z.substr(i,1);if(zt==','){tt=','+tt}
    if(zt=='#'){tt=dx.substr(jk,1)+tt;jk=jk-1;}}}
    rst.innerText=tt+cx
    }
    </script>
    </html>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    That code is absurdly long-winded. This topic has been fully covered elsewhere.

    Code:
    <script type="text/javascript">
    
    // works with negative numbers also
    function formatNumber(x){
    return x.toFixed(2).toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
    }
    
    var num = formatNumber(1234567.8976);
    num = "$" + num;
    alert (num);
    
    </script>

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    custom formatting numbers

    jayakodiu@yahoo.com

    Dear Sir, what about ###,##,##,###.## -- a format used in the sub continent for crores and lacs ? The available formatting snippets are only for millions.

    Well, the coding can be cryptic and short - but a new code has to be lucid for others to understand the algorithm.

    Have a nice day.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Well, the coding can be cryptic and short - but a new code has to be lucid for others to understand the algorithm.
    Okay. Please use [CODE][/CODE] tags to wrap your code while posting here, in this forum, to make it readable too (you may edit your above posts). Thanks.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •