Reputation: 117
I'm trying to calculate VAT from value in span, which works, but only if value inside span is used with dot. If i use comma, it shows NaN
JS
$(document).ready(function () {
$("#vatCalculate").ready(function () {
var vatTextBox = $("#vatPrice").text();
var op = (vatTextBox * 100) / 122;
var findVat = vatTextBox - op;
$("#vatValue").text(findVat.toFixed(2));
});
});
HTML
<span id="vatPrice">30.33</span>
<span id="vatValue"></span>
Tnx
Upvotes: 1
Views: 1328
Reputation: 8879
Thats because vatTextBox
is a string
, not a number.
Use parseFloat()
to parse it, and replace the ,
characters with a .
Upvotes: 0
Reputation: 2131
To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:
var vatTextBox = parseFloat(vatTextBox.replace(/,/g, ''))
You really should use hidden inputs instead as well and have it stored as a double.
Upvotes: 3