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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Changing decimal separater and having 2 decimal places

    Hi, Im working on a Order form for a friend in Germany (which is more challenging than I had hoped for)

    So the problem:
    I have set up everything so far that when a new item is selected the price (total) is also changed, My problem and question is how can I convert the decimal separater(from a dot to a comma as well as have 2 decimal places (eg 10.50 needs to be 10,50)), as everytime i try to add a .toFixed(2); it breaks the javascript. Im currently trying to go from one field that shows the total that and then one that shows the total with comma and decimal place...

    Trust me I have searched, ive tryed many differnt things but I just cant get it to work

    Heres the code behind the form

    Code:
    <script language="JavaScript">
    function calculateRadio(){
    
    var op1=document.getElementsByName('form[senfe][]');
    var op2=document.getElementsByName('form[oel][]');
    var result=document.getElementById('Total');
    
    result.value=0;
    result.value=parseInt(result.value);
    for(i=0;i<op1.length;i++)
    if(op1[i].checked) result.value=parseFloat(result.value)+parseFloat(op1[i].value);
    for(i=0;i<op2.length;i++)
    if(op2[i].checked) result.value=parseFloat(result.value)+parseFloat(op2[i].value);
    
    addComma();
    }
    
    function addComma(){
    
    var str=Total.value;
    var n=str.replace(/\./g, ",");
    comma.value=n;}
    
    </script>
    And a link to the form http://idee-genuss.de/index.php?opti...form&formId=13

    Its not pretty, wanted to get it working before I spent to much time on it.


  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it's unwise to refer to elements by their bare ID (although you can get away with it in most browsers). For toFixed() to work, you need to start off with a number. An input's value will always be a string...

    Code:
    var str=Number(document.getElementById('Total').value);
    var num=str.toFixed(2).replace(/\./g, ",")
    alert(num)
    or if you wanted to keep it all on one line for some reason:
    Code:
    var num=Number(document.getElementById('Total').value).toFixed(2).replace(/\./g, ",");
    alert(num)
    Last edited by xelawho; 09-27-2012 at 08:22 PM.

  • Users who have thanked xelawho for this post:

    xenix (09-27-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by xelawho View Post
    Code:
    var str=Number(document.getElementById('Total').value);
    var num=str.toFixed(2).replace(/\./g, ",")
    alert(num)
    More logical to name the variables

    Code:
    var num=Number(document.getElementById('Total').value);  //a number
    var str=num.toFixed(2).replace(/\./g, ",");  // now a string
    alert(str);
    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

    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:

    xenix (09-27-2012)

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much, you saved me from pulling out the last hair on my head



  •  

    Posting Permissions

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