Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Currency Formatting for 1000 separators including decimals

  1. #1
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Currency Formatting for 1000 separators including decimals

    Hi,

    How i need to perform currency formatting including decimal values, and it should not take invalid characters including +/- symbols and i can able to specify any number of decimals
    based on that it should display the currency format

    if i give as 123456 then the result should be 123456.00
    if i give as 123456.123 then the result should be 123,456.123
    if i give as 123456789.12345 then result should be 123,456,789.12345

    and should be in onblur only

    How can i do this can anybody help me

    Thanks&regards,
    latha Rao,

  2. #2
    Join Date
    May 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think String class and Array class will meet your requirement.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure how you want to deal with the +/- sign. It could be stripped, preserved, or rejected. Here it's rejected:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function formatThous(num){
    var f = formatThous;
    if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
    alert (f.errorMessage);
    return num;
    }
    var l, c = 1, t = '',
    sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
    num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
    l = num[0].length-1;
    while (l + 1){
    t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
    l--;
    c++;
    }
    return sign + t + (num[1] && num[1].length? '.' + num[1] : '');
    }
    // Configuration:
    formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
    formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
    formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
    </script>
    </head>
    <body>
    <input type="text" onblur="this.value = formatThous(this.value);">
    </body>
    </html>
    But I've left in the code that (with a few minor configuration changes) could be used to either strip or to preserve a leading + or - symbol.
    Last edited by jscheuer1; 05-30-2008 at 03:59 PM. Reason: Add Configuration Options
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Currency Formatting for 1000 separators

    Hi John,

    Thanks a lot for helping the code is working fine

    but if i give the value as 12345 then it should display as 12,345.00

    and also if i do not give any value or if the textfield is empty it should not throw any error message



    Can u help me as i am a new for javascript



    thanks
    Latha Rao

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function formatThous(num){
    var f = formatThous;
    if (!num) return '';
    if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
    alert (f.errorMessage);
    return num;
    }
    var l, c = 1, t = '',
    sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
    num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
    l = num[0].length-1;
    while (l + 1){
    t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
    l--;
    c++;
    }
    return sign + t + (num[1] && num[1].length >= 2? '.' + num[1] : num[1] && num[1].length == 1? '.' + num[1] + '0' : '.00');
    }
    // Configuration:
    formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
    formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
    formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
    </script>
    </head>
    <body>
    <input type="text" onblur="this.value = formatThous(this.value);">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Currency Formatting for 1000 separators

    Hi John,

    You helped me alot in solving my big problem

    I need some restriction to provide for decimals

    if user wants only 4 decimals it should accept only 4 decimals if i give more than the given decimals it should throw an error message that
    "You should not exceed the given decimal digits!!!".

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, before we do that, how about rounding any numbers with more than 4 decimal places to 4 decimal places instead? I don't see any great need to scold the user for using more than 4, and they will quickly get the idea once they see what happens if they use more than 4.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Currency Formatting for 1000 separators

    Hi John,


    as per your reply, there should not be rounded if i give as 12345.12345 then
    it should be display 123,45.12345


    the format of calling the function should be like


    formatThous(num,decimalValues) {
    ..

    if(num[1].length > decimalValues) {
    alert("Invalid data: should not exceed "+decimalValues +"!!");
    }

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You like making your users mad, huh? (In email, often anything with two exclamation points [!!] is picked up by 'flame' filters.) The more the chance of 'being scolded' by a form (politely or otherwise), the less likely people will be to continue using it. A user friendly form will self correct where possible. Very well though, I at least toned down the language in the alert a little:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function formatThous(num, places){
    var f = formatThous;
    if (!num) return '';
    else if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
    alert (f.errorMessage);
    return num;
    }
    else if (num.toString(10).split('.').length > 1 && num.toString(10).split('.')[1].length > places){
    alert ('Invalid Data: Do Not Exceed ' + places + ' Decimal Places, Please.');
    return num;
    }
    var l, c = 1, t = '', 
    sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
    num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
    l = num[0].length-1;
    while (l + 1){
    t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
    l--;
    c++;
    }
    return sign + t + (num[1] && num[1].length >= 2? '.' + num[1] : num[1] && num[1].length == 1? '.' + num[1] + '0' : '.00');
    }
    // Configuration:
    formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
    formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
    formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
    </script>
    </head>
    <body>
    <input type="text" onblur="this.value = formatThous(this.value, 4);">
    </body>
    </html>
    If there are any more changes you would like, please make a complete list instead of asking for them one at a time.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ............

Bookmarks

Posting Permissions

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