2

How to round the factorial value in java scripts if we have below value

7.156945704626378 e +118

I want 7.16 only please let me know if we have any solution?

Salman A
  • 248,760
  • 80
  • 417
  • 510

4 Answers4

0

Put it in a String variable:

var x = "7.156945704626378e+118";

Then use a regular expresion which checks for the part before e and parse it back to float:

var y = parseFloat(x.match(/\d+[.][0-9]+/)[0]);

And round to 2 decimal points by:

var z = y.toFixed(2);

If you still want your e118 just multiply z with 1e118 then.

Here is a working fiddle

Igle
  • 4,572
  • 4
  • 30
  • 64
0
((Number(7.156945704626378e+118))/1.0e+118).toFixed(2);

try this

you can refer links

Formatting a number with exactly two decimals in JavaScript

and

How to convert a String containing Scientific Notation to correct Javascript number format

Community
  • 1
  • 1
The Hungry Dictator
  • 3,352
  • 5
  • 38
  • 48
0
var number=7.1569;
number*=100;
var new_number = Math.round(number);
new_number/=100;
Moussawi7
  • 11,026
  • 5
  • 35
  • 50
0

I am not sure whether this approach is correct but this give what user want in question.

Number((+num.toString().replace(/e.*/, '')).toFixed(2))

DEMO

var num = 7.156945704626378e+118;
console.log(Number((+num.toString().replace(/e.*/, '')).toFixed(2)));

# 7.16

Note: This assumes that exponent is not significant and mantissa is what all required for rounding off.

brg
  • 7,970
  • 9
  • 45
  • 60