Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

decimals is lost when parsing decimal value in jquery

I try to get a decimal value for further calculation in jquery.

        var vatPercentageString = $("#Price").val();
        var percentage = parseFloat(vatPercentageString).toFixed(2);

But even if I have the value "12,34" in the "Price" textbox I only get 12 as the percentage value, why is that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because , is not proper symbol for decimals separator. You should first replace it by .:

function changed() {

  var vatPercentageString = $("#Price").val().replace(',', '.');
  var percentage = parseFloat(vatPercentageString).toFixed(2);

  console.log(percentage);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input type="text" id="Price" onchange="changed()" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading