1

every time i use the Math.round(4.45678765e-6 * 10000)/10000 it gives me a 0 value but if i remove the e-6 it gives the correct answer 4.4567 what shoul i do? here's my code. the value has the power of 10 something like this 4.45678765x10^-6.

<html>    
<script type="text/javascript">

        var x = Math.floor (4.45678765 * 10000)/10000;
        document.write (x);

    </script>
</html>

is it even possible to limit the decimal places if the value has an exponent?

philip
  • 1,292
  • 3
  • 24
  • 44
  • possible duplicate of [Limit the decimal place in javascript is not working for 4.45678765e-6?](http://stackoverflow.com/questions/7319715/limit-the-decimal-place-in-javascript-is-not-working-for-4-45678765e-6) – Alex Bagnolini Sep 06 '11 at 12:26

5 Answers5

1

use 'numObj.toExponential([fractionDigits])'

your case is its raison d'être, and this overlooked gem seems to be a javascript method rarely given its due!

Quote from MDN:

Returns a string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point

More simply put, it limits the number of decimal places to show in exponential notation.

var x = 4.45678765e-6.toExponential(5) //returns 4.45679e-6
var x = 4.45678765e-6.toExponential(2) //returns 4.46e-6

See examples of more rounding with exponential notation here: https://stackoverflow.com/a/36532545/5440638

Community
  • 1
  • 1
Kay V
  • 3,738
  • 2
  • 20
  • 20
1

4.45678765e-6 is 0.00000445678765, that number with only five digits after decimal point is 0.00000 so JavaScript is giving you the correct result.

nobody
  • 10,599
  • 4
  • 26
  • 43
  • i thought the same thing, but he's multiplying by 10000, So it's really 0.0445678765, but it's still under 1, which will give 0 anyway using floor. – Matt Sep 06 '11 at 12:00
  • @Matt he's multiplying by 10000, rounding and dividing back by 10000 to limit digits after decimal point to 5 and everything is working just right, I don't see any problem here. – nobody Sep 06 '11 at 12:06
  • no i don't see a problem either, it's doing exactly what it should, but he thinks he can multiple by 10000 to get the 5 digits. I know what he wants to do, but it's obviously not going to work that way. He says if he leaves out the e-6 he gets the correct answer, so technically he wants to do (4.45678765e-6 * 1e6*10000) – Matt Sep 06 '11 at 12:10
  • then how can i make the answer look like this? 4.4567e-6 instead of 4.45678765e-6 or 0.000004457 because i need it to display with and exponent but it should display only four decimal places? – philip Sep 07 '11 at 09:43
1

try .toPrecision(5) instead. IE: (4.45678765).toPrecision(5)

Matt
  • 7,049
  • 7
  • 50
  • 77
0

You can use -

var x = Number((4.45678765 * 10000)/10000).toFixed(5);

The toFixed( <digit> ) will restrict your value to those digits after decimal point.

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
0

Not 100% sure what your after but if you want the power;

   var f = 4.45678765e-6;
   var exp = Math.floor(Math.log(Math.abs(f)) / Math.LN10);
   // -6
   var f2 = f * Math.pow(10, -exp);
   // 4.45678765
Alex K.
  • 171,639
  • 30
  • 264
  • 288