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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts

    format euro currency

    We format euro currency as 1.000.000,00 (just the opposite of USD).
    Anyone who could give me a script that changes the input into this format?
    I tried 'replace' with a regular expression, but my regexes don't work.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Code:
    function addSeparators(nStr) {
        nStr += '';
        var x = nStr.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? ',' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2;
    }
    
    // example call: addSeparators(1231231231.45)
    // will give 1.231.231.231,45

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thank you very much for your instantaneous reply.
    It works already very good, but could you make it foolproof too?
    There has to be a decimal comma followed by two digits.
    Input of 1234 should result in 1.234,00
    Input of 1234,5 should result in 1.234,50
    Input of 1234,123 should result in 1.234,12 or give a warning that there only can be 2 digits after the comma.
    Last edited by skynet; 01-19-2012 at 02:26 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    var num = 1234567890123.45345    //works with negative numbers as well 
    
    num = Math.round(num*Math.pow(10,2))/Math.pow(10,2);  // to 2 decimal places
    
    document.write('<span id="count"></span>');
    
    document.getElementById('count').innerHTML = addCommas(String(num));
    
    function addCommas(nStr) {
    nStr = nStr.replace(/[^0-9\.]/g,"");
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    nStr = "$" + nStr;
    if (nStr.indexOf('.') == -1) {  // if whole number add .00
    nStr = nStr + ".00";
    }
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.');  // display continental format
    return nStr;
    }
    
    </script>
    Or for values entered by the user:-

    Code:
    <input type = "text" name = "num" id = "num" onkeyup = "addCommas2(this.value)"><br><br>
    
    <span id="count1"></span><br>
    
    <script type = "text/javascript">
    
    function addCommas2(nStr) {
    nStr = nStr.replace(/[^0-9\.]/g,"");  // strip non-numeric
    document.getElementById('num').value = nStr;
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.');  // display continental format
    document.getElementById('count1').innerHTML = nStr;
    }
    
    </script>
    If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.
    Last edited by Philip M; 01-19-2012 at 04:39 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    skynet (01-19-2012)

  • #5
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    It works like a dream.
    Concerning the foolproof parts:
    Input of 1234 should result in 1.234,00: I found this in your first example.
    Input of 1234,5 should result in 1.234,50
    Input of 1234,123 should result in 1.234,12 or give a warning that there only can be 2 digits after the comma.
    : I'll try to add this myself.
    Many, many thanks !

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Add this line in blue:-

    Code:
    function addCommas2(nStr) {
    nStr = nStr.replace(/[^0-9\.]/g,"");  // strip non-numeric
    nStr = nStr.replace(/(\.\d)(\d)(\d+)$/,"$1$2");  // truncate input to 2 decimal places only
    document.getElementById('num').value = nStr;
    and this

    Code:
    nStr = nStr.replace(/(\.\d)$/, "$10");  // show 2 decimal places with final 0 if only one entered 
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.');  // display continental format
    document.getElementById('count1').innerHTML = nStr;
    Instead of the $ sign you will want to use &euro;
    Last edited by Philip M; 01-19-2012 at 05:50 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    skynet (01-20-2012)

  • #7
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Much obliged. Thank you very much for your help.
    The last thing that doesn't seem to work is:
    nStr.replace(/(\.\d)$/, "$10"); // show 2 decimal places with final 0 if only one entered
    Entering 1234.5 results in 1.234,5 instead of 1.234,50

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by skynet View Post
    Much obliged. Thank you very much for your help.
    The last thing that doesn't seem to work is:
    nStr.replace(/(\.\d)$/, "$10"); // show 2 decimal places with final 0 if only one entered
    Entering 1234.5 results in 1.234,5 instead of 1.234,50
    It works for me and I get 1.234,50

    You may care to add another line to cover the case where the user enters a decimal point with nothing after it.

    nStr = nStr.replace(/\.$/,".00"); // add 00 to decimal point with no digits following
    nStr = nStr.replace(/(\.\d)$/, "$10"); // show 2 decimal places with final 0 if only one entered
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.'); // display continental format

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    skynet (01-20-2012)

  • #9
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I admire your knowledge of regular expressions and do believe it works for you.
    I tried this in several browsers but my result stays the same: entering 1234.5 gives 1.234,5 instead of 1.234,50.
    I respect your precious time, but could you please post here the complete page you are using, so I can exclude possible errors I made in mine ? At the moment, I can't find any in my page.
    You did help me a lot already, and want to thank you for this.
    Last edited by skynet; 01-20-2012 at 12:51 PM.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    <input type = "text" name = "num" id = "num" onkeyup = "addCommas2(this.value)"><br><br>
    
    <span id="count1"></span><br>
    
    <script type = "text/javascript">
    
    function addCommas2(nStr) {
    nStr = nStr.replace(/[^0-9\.]/g,"");  // strip non-numeric
    nStr = nStr.replace(/(\.)(\d+)?(\.)/,"$1$2");  // only one decimal point
    nStr = nStr.replace(/(\.\d)(\d)(\d+)$/,"$1$2");  // truncate to 2 decimal digits only
    document.getElementById('num').value = nStr;  // write entry back to the field
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    
    if (nStr.indexOf(".") == -1) {
    nStr += ".00";   // add .00 to whole number (if desired)
    }
    
    nStr = nStr.replace(/\.$/,".00");  // add 00 to decimal point with no digits following
    nStr = nStr.replace(/(\.\d)$/, "$10");  // show 2 decimal places with final 0 if only one entered
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.');  // display continental format
    document.getElementById('count1').innerHTML = nStr;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    skynet (01-20-2012)

  • #11
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Great, your code works.
    I can't see any difference with my code right away, but there has to be some error in mine.
    I already did find a solution for this problem with
    if (nStr.charAt(nStr.length-2) == '.') {nStr = nStr+'0';}
    That works too, but it's not so nice coded as yours.
    Thanks again, I appreciate your help very very much ! You did help me a lot, and saved me from a severe headache.
    Last edited by skynet; 01-20-2012 at 03:15 PM.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Glad you have got it to work! Even a space in the wrong place can prevent a regular expression from working.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    skynet (01-20-2012)

  • #13
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    A final question.
    An input of our correct format (f.e.: 1.234,56) gives 1,23 as result.
    How could I prevent this ?

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Oh dear! You are not supposed to place anything but digits and a decimal point (.) in the input field. You may not enter a decimal point as a separator, as in European style. The dot . means a decimal point. (12.34)

    You are entering 1.23 (one point two three) and so your requirement that only 2 decimal digits are allowed does not allow you to enter any more digits. You cannot enter 1.2345 or of course 1,23 as the comma is ignored.

    If you want the user to enter a number with separators (1,234,567) then the whole thing is pointless. The purpose of the script is to format the number by adding the commas (separators) to the plain number 1234567.89, and in your case change the . to a , and the , to a . in the final output. That is what this line does:-

    Code:
    nStr = nStr.replace(/,/g, '#').replace(/\./g, ',').replace(/#/g, '.');  // display continental format
    If you wish to allow input of the decimal radix point as a comma (,) then you will need to rewrite the script accordingly, replacing evey literal . (\.) with a literal , (\,) So:

    Code:
    <input type = "text" name = "num" id = "num" onkeyup = "addCommas2(this.value)"><br><br>
    
    <span id="result"></span><br>
    
    <script type = "text/javascript">
    
    function addCommas2(nStr) {
    nStr = nStr.replace(/[^0-9\,]/g,"");  // strip non-numeric (decimal is ,)
    nStr = nStr.replace(/(\,)(\d+)?(\,)/,"$1$2");  // only one decimal comma
    nStr = nStr.replace(/(\,\d)(\d)(\d+)$/,"$1$2");  // truncate to 2 decimal digits only
    document.getElementById('num').value = nStr;  // write entry back to the field
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1.$2');
    }
    
    // display continental format
    
    if (nStr.indexOf(",") == -1) {
    nStr += ",00";   // add .00 to whole number (if desired)
    }
    
    nStr = nStr.replace(/\,$/,",00");  // add 00 to decimal point with no digits following
    nStr = nStr.replace(/^\,00/,"");  // if entry backspaced out
    nStr = nStr.replace(/^\,/,"0,");  // add leading zero if nothing before decimal radix
    nStr = nStr.replace(/(\,\d)$/, "$10");  // show 2 decimal places with final 0 if only one entered
    
    document.getElementById('result').innerHTML = nStr;
    }
    
    </script>
    Last edited by Philip M; 01-21-2012 at 04:41 PM. Reason: Correct error and make improvement

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    New Coder
    Join Date
    Sep 2005
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    The script doesn't need to be rewritten, I'll try to explain it.

    The only correct format for us is 1.234,56
    On a keypad there isn't a decimal comma, but a decimal point; therefore some people give 1234.56 as input.
    Your script corrects this to the right format: 1.234,56
    It corrects also the ones who forget the 2 digits after the decimal comma.

    The last problem is that if someone already gives the correct input (1.234,56) the script changes this into an unwanted value.

    I'm trying to avoid this by inserting as first rule in the script:
    IF
    (nStr.charAt(nStr.length-3) == ',') // decimal comma at the right place
    AND
    (nStr.charAt(nStr.length-2).match(/[^0-9]/)) // followed by a digit
    AND
    nStr.charAt (nStr.length-1).match(/[^0-9]/)) // followed by another digit
    THEN
    the input is OK and the remaining of the script shouldn't be executed.


    Of course, I have not the appropriate skills to make this work.
    Would you please be so kind to help me again ?
    I am a bit ashamed, and want to thank you already for your patience with me.
    Last edited by skynet; 01-21-2012 at 03:46 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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