I need to convert 'KM1+000.321' to 1000.321.
I tried this
var string = 'KM1+000.321';
console.log(parseInt(string.replace(/\D/g, ''), 10)) //1000321
But that also removes the decimal. How do I write an expression that keeps the decimal?
http://jsfiddle.net/bbn80knc/
Assuming that there will only be one decimal, this will work:
var string = 'KM1+000.321';
console.log(parseFloat(string.replace(/[^\d.]/g, ''))) //1000.321
Note, you need to use parseFloat, not parseInt, to get the decimals.
To replace all but the digits and the decimal dot with nothing, use
string.replace(/[^\d.]/g, '')
To convert to a number with a fraction, you can't use parseInt() but parseFloat():
parseFloat(string.replace(/[^\d.]/g, ''))
Related
In a javascript code, I have a requirement to format a decimal number to a specific number of decimal places and get its exact string representation. For example, If the number is 999999999.9 and the number of decimal places is 8, then the expected value should be "999999999.90000000"
When the Number.toFixed(8) is used it returns a rounded value which is not what I want. Please refer the below code
var num = 999999999.9
var string_rep = num.toFixed(8)
>> the value of string_rep is "999999999.89999998"
I used num.toString() and tried to manually format the decimal part by adding/removing digits, but it does not work for very small numbers like "0.00000008" as the function toString() returns
the scientific notation, i.e. something like "9e-8"
So what should be the proper approach for this?
Number.prototype.toLocaleString will do the trick
num.toLocaleString('en-US', {minimumFractionDigits: 8, useGrouping: false})//"999999999.90000000"
I have 14 Digit Number with the leading zero's , but when i am doing parseInt on that then it is removing leading zero's.
var number = parseInt("00000011121314");
console.log("Number after parsing is "+ number);
It is giving me answer as a 11121314 instead of 00000011121314..
The problem is that "00000011121314" is not actually a number, „11121314“ is. The parseInt() function cuts the leading zeros to convert the string, which would allow for storing leading zeros, as a string is only an array of characters. If you need to store or work with the leading zeros, you could use a string, if you can live without them and you can use integer for storing.
To be clear: there is no way to store leading zeros in an integer, as the datatype was constructed to handle numbers.
How can I remove all decimals from a number? I want to get the number as specified below. I need to remove decimal points only. I am not getting the logic for it.
If number x= 1.1.6;
then I want result as 116
and when x=0.0.6;
then I want result as 6.
Since 1.1.6 is not a valid numerical value in JavaScript, I assume that you're starting with a string. You can get the result as an integer value with:
parseInt(number.replace(/\./g, ''))
If desired, you can then turn that back into a string with no leading zeroes with:
'' + parseInt(number.replace(/\./g, ''))
try this. replace all the . using replace method and convert to integer
document.write(parseInt("1.1.6".replace(/\./g, '')))
document.write('<br>')
document.write(parseInt("0.0.6".replace(/\./g, '')))
I get the total_amount in cart code of ecommerce web page, after I use toString function, but when I convert the 14.82 string to int number, the decimals disappear.
<script>
var total_amount_string = <?=json_encode($cart['total_pvp'])?>;//-> 14,82
var total_amount_int = parseInt(total_amount_string).toFixed(2); //-> 14.00(here is the error)
console.log(total_amount_string) //-> 14,82
console.log(total_amount_int) //-> 14.00
</script>
What's the matter?
If the input string is "14,82" and you want the value 14.82, you'll need to convert the , to a . and then use parseFloat:
var total_amount_int = parseFloat(
total_amount_string.replace(/,/g, ".")
).toFixed(2);
parseInt will only parse the leading part of the string that defines a whole number ("int" = "integer" = "whole number"), so it stops at the ,. parseFloat will parse a decimal number, but only understands . as the decimal point, not ,, even in locales where , is the decimal point.
you should use parseFloat() instead of parseInt(). Because integer number is not decimal.
An integer has no decimal part, so any number "casted" to int will lose its decimals. You should use parseFloat if you want to keep the decimal part.
On the other hand, make sure you are not using a comma: Javascript parse float is ignoring the decimals after my comma
Your number is using a "," separator. Unfortunately there is no locale settings for number parsing in JavaScript, so you are forced to a bit more hacky:
var total_amount_string = "14,823462";
var total_amount_float = parseFloat(total_amount_string.replace(",", ".")).toFixed(2);
document.getElementById("total_amount_string").innerText = total_amount_string;
document.getElementById("total_amount_float").innerText = total_amount_float;
total_amount_string: <span id="total_amount_string"></span>
<br />
total_amount_float: <span id="total_amount_float"></span>
The parseInt() function parses a string and returns an integer. ParseInt returns only the first number in the string is returned!. If the first character cannot be converted to a number, parseInt() returns NaN.
I'm doing this:
var refundAmount = parseFloat($('#refundAmount2').val().replace('$',''));
var refundReceived = $('#refundReceived');
var remainderAmount = refundAmount-parseFloat(refundReceived.val().replace('$',''));
alert(parseInt(remainderAmount).toFixed(2));
No matter what I do, the result always ends with 2 decimal places being '.00'. So if the first number is 200.12 and the second is 100.08, it should be alerting me with 100.04 but instead I get 100.00.
Why might this be happening?
You used parseInt to convert that number to an integer and then used toFixed(2) to convert it to a number with 2 decimal places. Adding 2 decimal places to an integer will always result in .00.
Try
alert(remainderAmount.toFixed(2));
See DEMO.
You're getting it as an int with parseInt(), then doing the toFixed(). So you're putting decimal places on an int.