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
Related
i have a string like this 21600.00 how do I delete 00 after the dot?
I've tried it like this
replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
but '.00' is not erased, so the assumption that I expect is like this 21,600 or 8,000 etc.
You could try any of these:
const str = '21600.00'
console.log(new Intl.NumberFormat('en-US').format(str));
console.log(Number(str).toLocaleString('en-US'))
console.log(str.slice(0,-3))
console.log(str.split('.')[0])
console.log(str.substring(0, str.indexOf('.')))
console.log(`${Math.round(str)}`)
console.log(`${Math.trunc(str)}`)
console.log(`${parseInt(str)}`)
console.log(Number(str).toFixed())
You can simply use parseInt function to remove .00 This will show the exact number without any zeros added.
If you want to convert back to string format you can use toString() to do that.
If you want to add commas format you can use toLocaleString
Run snippet below.
let string = '21600.00'
//Use parse Int
let int = parseInt(string)
console.log(int.toString()) //Returns string
console.log(int) //Returns number
console.log(int.toLocaleString()) //Returns number with comma added
You can try this.
var decimal = 21600.00;
var numString = decimal.toString().split('.')[0];
var integer = parseInt(numString);
console.log(integer);
console.log(Number(numString).toLocaleString('en-US'));
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'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('');
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,""));
I have a bunch of strings extracted from html using jQuery.
They look like this:
var productBeforePrice = "DKK 399,95";
var productCurrentPrice = "DKK 299,95";
I need to extract the number values in order to calculate the price difference.
(So I wend up with ≈
var productPriceDiff = DKK 100";
or just:
var productPriceDiff = 100";)
Can anyone help me do this?
Thanks,
Jakob
First you need to convert the input prices from strings to numbers. Then subtract. And you'll have to convert the result back to "DKK ###,##" format. These two functions should help.
var priceAsFloat = function (price) {
return parseFloat(price.replace(/\./g, '').replace(/,/g,'.').replace(/[^\d\.]/g,''));
}
var formatPrice = function (price) {
return 'DKK ' + price.toString().replace(/\./g,',');
}
Then you can do this:
var productBeforePrice = "DKK 399,95";
var productCurrentPrice = "DKK 299,95";
productPriceDiff = formatPrice(priceAsFloat(productBeforePrice) - priceAsFloat(productCurrentPrice));
try:
var productCurrentPrice = productBeforePrice.replace(/[^\d.,]+/,'');
edit: this will get the price including numbers, commas, and periods. it does not verify that the number format is correct or that the numbers, periods, etc are contiguous. If you can be more precise in the exact number definitions you expcet, it would help.
try also:
var productCurrentPrice = productBeforePrice.match(/\d+(,\d+)?/)[0];
var productCurrentPrice = parseInt(productBeforePrice.replace(/[^\d\.]+/,''));
That should make productCurrentPrice the actual number you're after (if I understand your question correctly).