How to replace a comma for a dot and a dot for a comma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vabby
    New Member
    • Mar 2007
    • 4

    How to replace a comma for a dot and a dot for a comma

    Hey guys.
    I have a problem with my code, I can replace comma for a dot here. "var tvn = parseFloat(docu ment.getElement ById('ctl00_Con tentPlaceHolder 1_txtMillilTVNH raefni').value. replace(/,/g, '.')*14);" and multiply with 14, that works fine, but i can't replace a dot for a comma before I display the sum. In me country we use comma instead of a dot for separating decimal numbers

    For example: a user has to be able to import 1,25 in a text box on my web site which changes to 1.25 before it's multiplied with 14 to get the sum of 17.5. But I have to change it to 17,5 before displaying. (which isn't working somehow)


    Here is my code:

    Code:
    function calculateTVN() 
              {  
                if (document.getElementById    ('ctl00_ContentPlaceHolder1_txtMillilTVNHraefni').value != "")
                {
                  var tvn = parseFloat(document.getElementById('ctl00_ContentPlaceHolder1_txtMillilTVNHraefni').value.replace(/,/g, '.')*14); 
                  var tvns = tvn.replace (/\./g, ',');
     document.getElementById('ctl00_ContentPlaceHolder1_txtNidurstTVNHraefni').value = tvns.toFixed(2);
                }
              }
    It would be nice if somebody could help me.
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by vabby

    Code:
       var tvn = parseFloat(document.getElementById('ctl00_ContentPlaceHolder1_txtMillilTVNHraefni').value.replace(/,/g, '.')*14); 
                  var tvns = tvn.replace (/\./g, ',');
    I would prefer to multiply after the conversion:
    Code:
       var tvn = parseFloat(document.getElementById('ctl00_ContentPlaceHolder1_txtMillilTVNHraefni').value.replace(/,/g, '.'))*14;
    but you should test the value returned from parseFloat with isNaN.

    tvn is a number and therefore does not have a replace method.

    Try
    Code:
     var tvns = tvn.toString().replace (/\./g, ',');

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      [font=Verdana][size=2]
      Code:
      [/size][/font]
      <script>
      function calculateTVN() { 
       if (document.getElementById ("ctl00_ContentPlaceHolder1_txtMillilTVNHraefni").value != "") {
         var tvn = parseFloat(document.getElementById("ctl00_ContentPlaceHolder1_txtMillilTVNHraefni").value.replace(/,/gi, '.')*14); 
         tvn += ""; // convert it to a string instead of integer to do the regular expression.
         var tvns = tvn.replace(/\./gi, ",");
         document.getElementById("ctl00_ContentPlaceHolder1_txtNidurstTVNHraefni").value = tvns;
       }
      } 
      </script>
      <input type="text" id="ctl00_ContentPlaceHolder1_txtMillilTVNHraefni"><input type="text" id="ctl00_ContentPlaceHolder1_txtNidurstTVNHraefni"><input type="button" value="calculate" onclick="calculateTVN();">
      This is how i got it to work,

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        logician beat me to it.

        Comment

        • vabby
          New Member
          • Mar 2007
          • 4

          #5
          Thank you very much guys. It works perfectly ;)

          Comment

          • vabby
            New Member
            • Mar 2007
            • 4

            #6
            Hey guys,
            Change comma fore a dot and a dot for a comma works perfectly.
            Now I have a problem with some numbers, for example if user put in 1,9 it displays 26,59999, I have lost the function .toFixed(2) in “document.getEl ementById('ctl0 0_ContentPlaceH older1 _txtNidurstTVNH raefni').value = tvns.toFixed(2) ;” do you know how to fix that.

            Thanks again.

            Comment

            • vabby
              New Member
              • Mar 2007
              • 4

              #7
              Code:
              function calculateTVN() 
                          { 
                           if (document.getElementById ("ctl00_ContentPlaceHolder1_txtMillilTVNHraefni").value != "") 
                           {
                                  var tvn = parseFloat(document.getElementById("ctl00_ContentPlaceHolder1_txtMillilTVNHraefni").value.replace(/,/gi, '.'))*14;
                                  var tvna  = tvn.toFixed(2);
                                  //tvn += ""; // convert it to a string instead of integer to do the RE
                                  var tvns = tvna.toString().replace(/\./gi, ",");
                                  document.getElementById("ctl00_ContentPlaceHolder1_txtNidurstTVNHraefni").value = tvns;
                           }
                       }
              It works like that TANKS

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                whoops forgot i took the tofixed out of it i was going to move it somewhere else in the code because to fixed won't work with a ,

                glad you figured it out. :)

                Comment

                • sivadhanekula
                  New Member
                  • Nov 2008
                  • 58

                  #9
                  Code:
                  int num=0, p=0,d=0;
                  sscanf(num,"%d.%d",&p,&d) ; 
                  sprintf(num,"%d,%d",p,d) ;
                  Dont forget to include #include<string .h>
                  Last edited by acoder; Nov 14 '08, 05:54 PM. Reason: Added [code] tags

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    This is a thread in the JavaScript forum and you've given code for C - two very different programming realms. Anyway, thanks for your input.

                    Comment

                    • neovantage
                      New Member
                      • Aug 2008
                      • 245

                      #11
                      Thanks for this great discussion. it really helps me to sort out my problem too. As i also need to show comma instead of dot as dot does not recognise in Germany.

                      Comment

                      Working...