I have a number which is displaying as follows:
1.0333333333333335e-9
I'd like to limit the number of digits that's shown, so it looks more like:
1.03e-9
How do I do this?
numObj.toExponential(fractionDigits) has it built in
fractionDigits
An integer specifying the number of digits after the decimal point.
Defaults to as many digits as necessary to specify the number.
Related
I could make Javacsript regex with 2 decimal points with 3 integer but the thing is I have to make it 6 digits total including the decimal "dot".
So this result should be a minimum 0 to 999.99 with the condition it's a minimum 0 to 6 digit length.
Below is my solution So far:
^(\d{0}(?:\d{1,3})|\d{1}\.(?:\d{1,2})|\d{2}\.(?:\d{1,2})|\d{3}\.(?:\d{1,2}))$
Explanation:
If it's 0 digits, then min 1 to 3 digits so I can make it max 999.
If it's 1 digit w/ decimal then max 2 decimal points (ex) 2.22
If it's 3 digits w/ decimal then max 2 decimal points. (ex) 3.33
This is for regex in my JSP input (which is text type) and I'm literally suffering for this problem for days.
(this regex is for versions to be specific..)
Any help or better alternative way would help me A LOT.
google search, try by myself, online course, Youtube
If I get it well: you need to match a decimal with a length of 6 including the decimal point.
The following can do:
^(\d{1,3}(?:\.\d{1,2})?)$
It specified a integer part up to 3 digits, then an optional decimal part.
It has some edge cases of course such as matching: 000.00 which can be cleaned up further if needed depending on your expected input.
Sample: https://regex101.com/r/LrOHvt/1
What way i can convert string with 16 digits and 2 fraction value to number ?
Currently when I try to convert Number('1234567890123456.12') will became to 1234567890123456. fraction values will be gone.
I just want to confirm without using any third party lib can i convert this string to number ?
Unfortunately not. Javascript represents it's numbers using double precision floating point numbers. At 16 digits, it will only be able to store the integer component and not the part after the decimal point. You will need a bignum library to use this value.
EDIT: for reference the biggest integer you can use in JavaScript is 9,007,199,254,740,991
EDIT2: Thanks to Jeremy you can use a library like bignumberJS.
Your number has too many algorisms, I've created an example that simulates in the first position of the array the maximum length possible in javascript.
var nums = [
"12345678910111.12",
"1.5323",
"-42.7789"
];
nums.forEach(function(n) {
console.log(parseFloat(n).toFixed(2));
});
https://jsfiddle.net/7zzz1qzt/
I have faced issue to convert 18 digit string number to number. It is convert all digit to 0 after 16 digit. I have apply below code. it is working fine for me.
[{"id":${id},"name":"${name}"}]
I have a regular expression:
/^(([1-9]+\.[0-9]*)|([1-9]*\.[0-9]+)|([1-9]+))([eE])([-+]?[0-9]+)?$/
to validate exponential value.it is working fine.But some values like 21E,31E it is considering as exponential value but values like 09E it is considering non exponential value.
Can we have the solution for this ,So that it ll consider all values like 21E,31E,09E as non-exponential value.
Thanks
A mantissa is either a 0, or a nonzero digit possibly followed by digits.
0|([1-9][0-9]*)
A fractional part is a possibly empty string of digits.
[0-9]*
A real number is a mantissa followed by a point followed by a fractional part.
(0|([1-9][0-9]*))\.[0-9]*
An exponent is e followed by a mantissa.
(eE)(0|([1-9][0-9]*))
A scientific number is a real number optionally followed by an exponent
(0|([1-9][0-9]*))\.[0-9]*([eE](0|([1-9][0-9]*)))?
Some variants are possible, as the OP didn't give a complete specification.
I have a simple conversion form from kg to lbs.
html
<input type="text" id="kg" name="kg">
<input type="text" id="lbs" name="lbs">
I have it setup so that the lbs box updates while you type in the kg box with this code.
jQuery
$("#kg").keyup(function(){
$('#lbs').val($('#kg').val()*2.20462);
});
How do I get the lbs value to round to 2 decimals places? I am sure it is something fairly simple but all the examples I found online are for if the number is stored in a variable.
Use toFixed
var string = yourNumber.toFixed(2);
use toFixed:
$('#lbs').val(($('#kg').val()*2.20462).toFixed(2));
number.toFixed( [digits] )
Parameter
digits The number of digits to appear after the decimal point; this
may be a value between 0 and 20, inclusive, and implementations may
optionally support a larger range of values. If this argument is
omitted, it is treated as 0.
Returns
A string representation of number that does not use exponential
notation and has exactly digits digits after the decimal place. The
number is rounded if necessary, and the fractional part is padded with
zeros if necessary so that it has the specified length. If number is
greater than 1e+21, this method simply calls Number.toString() and
returns a string in exponential notation.
also this
(10.8).toFixed(2); // 10.80
var num = 2.4;
alert(num.toFixed(2)); // 2.40
Formatting a number with exactly two decimals in JavaScript
I set up a system that parses a compact data string into JSON. I'm using a 19 digit number to store ids. Unfortunately any number greater than 17 digits, parseFloat() rounds the last few digits.
This breaks the whole data string. Can I fix this?
For example 8246295522085275215 gets turned into 8246295522085276000. Why is this?
http://jsfiddle.net/RobertWHurst/mhZ7Q/
JavaScript has only one numeric type, which is an IEEE 754 double precision floating-point. That means, you have a maximum of 52 bits of precision, which is a bit more than 15 decimal places.
If you need more precision than that, you have to use a bignum library or work with strings.
Numbers in JavaScript lose precision if they are higher than a certain value.
According to http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference, integers are only reliable up to 15 digits (9 * 10^15 to be exact).
Try one of these
1. Use a string
2. Split your number in two and save the smaller parts to an array
3. Bignum library
4. Use a smaller number if you can