I have this large number:
21212202382541035306949186015338569645685453497668055588619610488832
Javascript automatically shortens it for me like this:
2.1212202382540943e+67
But since I put that inside a table cell with a fixed with, I need the number to be less or equal then 11 digits in total.
So in example, if I have the previous mentioned number, it should be converted to:
~2.1212e+67
And, if the number is:
128330558031338336 it should show ~1.2833e+17
So basically, if the length of a number is longer then 11 digits, it should start adding the powers of 10 to it and also start rounding the number.
How can I do this withing JavaScript?
I tried with substring()
, but I have problems determining the exact powers to be shown.
Edit
The end script looks like this:
if ( num.length > 11 ) {
num = parseFloat( num );
var n = '~'+ num.toExponential( 4 );
} else {
var n = num;
}
Thanks a lot Black Maggie
I have this large number:
21212202382541035306949186015338569645685453497668055588619610488832
Javascript automatically shortens it for me like this:
2.1212202382540943e+67
But since I put that inside a table cell with a fixed with, I need the number to be less or equal then 11 digits in total.
So in example, if I have the previous mentioned number, it should be converted to:
~2.1212e+67
And, if the number is:
128330558031338336 it should show ~1.2833e+17
So basically, if the length of a number is longer then 11 digits, it should start adding the powers of 10 to it and also start rounding the number.
How can I do this withing JavaScript?
I tried with substring()
, but I have problems determining the exact powers to be shown.
Edit
The end script looks like this:
if ( num.length > 11 ) {
num = parseFloat( num );
var n = '~'+ num.toExponential( 4 );
} else {
var n = num;
}
Thanks a lot Black Maggie
Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Mar 15, 2013 at 8:40 PeonPeon 8,0407 gold badges61 silver badges104 bronze badges2 Answers
Reset to default 7Source: https://developer.mozilla/en-US/docs/JavaScript/Reference/Global_Objects/Number/toExponential
There is a toExponential() in javascript
So if you want to have a total of 11 characters remaining in the exponential representation, you may pass the number of digits remain after the decimal places
toExponential(5)
may results 2.12122e+67
for your number 21212202382541035306949186015338569645685453497668055588619610488832
Update: selected a better source from mozilla instead of w3school. Thanks @T.J. Crowder
Update 2: Source:http://www.php/manual/en/language.types.float.php
For php, use round()
seems possibly force a scientific notation conversion on the number.
I think in your case toPrecision would be more appropriate.
You may need some trickery to fit your table cell if your numbers deviate greatly. For example try with 8 significant digits, test string length, and if too long, try again with 7 and perhaps even 6 and 5.
Update: Ok, I played a bit with the following code:
num = new Number(<your num>);
alert("prec: " + num.toPrecision(5) + " exp: " + num.toExponential(4));
Sample output:
prec: 364.00 exp: 3.6400e+2
prec: 3.6435e+13 exp: 3.6435e+13
I would say it depends on if you want to always see exponential numbers or only when needed. In the former case, toPrecision() is better because it leaves the number fixed point if possible so you can get higher precision in the same string length. If you want always exponential numbers, then toExponential() is better. if your numbers will always be big enough to be exponential, then it doesn't matter.