Reputation: 70
I have a string that I am getting from a Jquery selector .html that is
example
<span class="currency">$</span>" 477,000.00"
I want to just get the 477000.00 value so that I can use this as a number for some calculations.
I tried a parseInt and it returns Nan.
Here is my code for selecting:
Here is my actual code:
function getSystemPrice() {
var currentSystemPrice = $("#bom-div-content table tbody tr td").html();
currentSystemPrice = parseInt(currentSystemPrice);
$("#SYSTEM_PRICE_TILES_ONLY").val(currentSystemPrice);
}
Upvotes: 0
Views: 80
Reputation: 66470
try:
var string = '<span class="currency">$</span>" 477,000.00"';
var output = parseFloat(string.match(/([\d,.]+\.)/)[1].replace(/,/g, ''));
document.getElementById('output').innerHTML = output;
<div id="output"></div>
UPDATE:
var string = '<span class="currency">$</span>" 477,000.00"';
var string2 = '<span class="currency">$</span>" 12.477.000,00"';
var re = /((?:\d{1,3}[,.]?)+)[,.](\d{2})/;
var m = string.match(re);
var output = document.getElementById('output');
output.innerHTML += parseFloat(m[1].replace(/[,.]/g, '') + '.' + m[2]) + '\n';
m = string2.match(re);
output.innerHTML += parseFloat(m[1].replace(/[,.]/g, '') + '.' + m[2]);
<pre id="output"></pre>
Regex explanation:
(
(?: non capturing group
\d{1,3} 1 to 3 digits
[,.]? optional comma or dot
)+ at least one of those
)
the whole is wrap in parenthesis so it capture the whole thing (number before last comma or dot)[,.]
the last comma or dot (not captured)(\d{2})
capturing group that match last 2 digitsUpvotes: 3
Reputation: 910
Do you want to parse the number as an int or a float, as there's two ways to do this.
To parse as an int, you should pass a second argument to parseInt, the radix (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).
parseInt("10", 10);
This is because most implementations use 10 as a default radix, but NOT all, so it might be a subtle source of bugs later down the line.
Taking the jQuery out of it and simply reading the number in as a string and converting it to a float, I did like this:
var tmp = "<span class='currency'>$</span> 477,000.00".split(" ")[1];
parseFloat(tmp.split(",").reduce((a, b) => a + b ));
You could of course use parseInt instead of parseFloat as explained earlier, depending on the exact nature of the data you want.
Not sure if it's the solution you are looking for but I thought I'd see if I could get it done using reduce, I don't mind admitting I learned something here too.
Upvotes: 0
Reputation: 729
Try this.
function getSystemPrice() {
var currentSystemPrice = $("#bom-div-content table tbody tr td").html();
currentSystemPrice = currentSystemPrice.replace("$",""); //Here you take out the $
currentSystemPrice = currentSystemPrice.replace(" ",""); //Here you take out white spaces
currentSystemPrice = parseFloat(currentSystemPrice); //This way you will have decs if its needed
$("#SYSTEM_PRICE_TILES_ONLY").val(currentSystemPrice);
return true;
}
Upvotes: 0
Reputation: 2200
What are the possible values you get? If you always get "$" " you can split the string on the ; str.split(";"). If you know the number is always the last part of the string, you must pick of the characters from the end (using e.g. str.slice(-1) and stop when you get characters that does not make sense as a number.
Upvotes: 0