I have a number that I need to pull from my html:
<span>123,456.78</span>
How can I convert this string into a number that I can do math on?
var numberString = $('span').text();
var realNumber = Number(numberString); //returns NaN
A jQuery-only solution would be okay.
parseInt() or parseFloat() would just about do it.
var number = parseFloat($('span').text());
after checking and seeing this doesn't work...
try
var number =
$('span').text().replace(/([^0-9\.])/g,"");
var number = parseFloat($('span').text().replace(/([^0-9\\.])/g,""));
I'm not sure what realNumber does, but here's how I'd convert that string into a number:
var numberString = $('span').text();
var amount = + numberString.replace(/,/g, '');
This removes the commas, then uses the + unary operator to convert the string to a number. In your example, the result is the number 123456.78.
updated
var numberString = $('span').text();
var number = Number(numberString.replace(/[^0-9\.]+/g,""));
Related
I know that if either one of my operands is a string, it should prefer string concatenation, but I get an integer.
var number = 134324;
var num_str = number + "";
console.log(num_str);
No it should not return string .. as you are printing it in console it looks like integer but try using
var number = 134324;
var num_str = number + "";
console.log(num_str);
typeof(num_str);
It will display that your answer is string ... :) hope you satisfied ..
You can use toString() method.
num_str = number.toString()
I have a string:
var example = 'sorted-by-' + number;
Where number variable can be any positive integer. I don't know how to reverse this process, not knowing how many digits this number has. I want to get from example string a number at the end.
var outputNumber = example.substring(10);
This is the simple solution because example string always start with 'sorted-by-'.
let num = + string.substr(10);
You can use String#replace function to replace sorted-by- to empty string and after that convert left part to a number:
var example = 'sorted-by-' + 125;
var num = +example.replace('sorted-by-', '');
console.log(num);
You can split string at - and get last element using pop().
var example = 'sorted-by-' + 100.99
var n = +(example.split('-').pop())
console.log(n)
You can also use regex for this.
var number = 245246245;
var example = 'sorted-by-' + number;
var res = example.match(/^sorted-by-(\d+)/);
console.log(+res[1]);
i am trying to parse a float in javascript in this way:
var number = '25.492.381';
var cost = parseFloat(number);
This returns 25.492
I wish this would return 25.492.381
How can this be done?
Remember that . in standard notation split the Integer and Fractional part of the number. You could want this:
var number = '25.492.381'.replace('.', '').replace(",",".") ;
var cost = parseFloat(number);
25.492.381 is not a float. You need to use a formatter.
Check Numbro
I'm using number_format function of PHP to format: 2100000 --> 2,100,000. Everything is OK
But when I using 2,100,000 to calcutate with javascript then I got a message: NaN.
So how can I solve this problem?
Thank you very much.
You can remove the commas from the number using a Regex
var myNumber = "2,100,000";
myNumber = parseInt(myNumber.replace(/\,/g,''), 10);
console.log(myNumber);
Show the formatted number but echo the unformatted number elsewhere and use that in js. For example:
PHP
<div id="number" data-myvalue="<?=$number?>"><?=number_format($number)?></div>
JAVASCRIPT
var myvalue = $("#number").data("myvalue");
"2,100,000" is a string. You'll need to remove "," so that it can be parsed by JavaScript and used for calculations. It's better to pass numbers around without custom formatting. However, if you receive data in such format, you can deal with them like so:
var a = "2,100,000";
a = a.replace(/,/g, ""); //Replace all occurences of "," with ""
a = parseInt(a); //If you know it's an integer
a = parseFloat(a); //If it might be a float
a += 1;
alert(a); //Displays 2100001
number_format returns 2,100,000 which is a string.
If you want to make other calculations with that in js, you will have to convert it to a integer( or float depending on what you need)
var number_string = '2,100,000';
number_string = string.replace(/[^0-9]/gi, ''); // remove non-numeric charachters
var number = parseInt(number_string); // parse the string to integer
Hope this helps.
You can use a split.join combination like this:
var numStr = "2,100,000";
var num = numStr.split(',').join('');
This will alert 23.
alert(parseInt('23 asdf'));
But this will not alert 23 but alerts NaN
alert(parseInt('asdf 23'));
How can I get number from like 'asd98'?
You can use a regex to get the first integer :
var num = parseInt(str.match(/\d+/),10)
If you want to parse any number (not just a positive integer, for example "asd -98.43") use
var num = str.match(/-?\d+\.?\d*/)
Now suppose you have more than one integer in your string :
var str = "a24b30c90";
Then you can get an array with
var numbers = str.match(/\d+/g).map(Number);
Result : [24, 30, 90]
For the fun and for Shadow Wizard, here's a solution without regular expression for strings containing only one integer (it could be extended for multiple integers) :
var num = [].reduce.call(str,function(r,v){ return v==+v?+v+r*10:r },0);
parseInt('asd98'.match(/\d+/))
function toNumeric(string) {
return parseInt(string.replace(/\D/g, ""), 10);
}
You have to use regular expression to extract the number.
var mixedTextAndNumber= 'some56number';
var justTheNumber = parseInt(mixedTextAndNumber.match(/\d+/g));
var num = +('asd98'.replace(/[a-zA-Z ]/g, ""));