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
  • 262,204
  • 82
  • 430
  • 521

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

Igl3
  • 4,900
  • 5
  • 35
  • 69
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,444
  • 5
  • 37
  • 53
0
var number=7.1569;
number*=100;
var new_number = Math.round(number);
new_number/=100;
Moussawi7
  • 12,359
  • 5
  • 37
  • 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.

Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60