3

I have parsing values to integer in my function using parseInt, but when i parse the -4.450217635509901e-7 to integer it returns -4.

parseInt(-4.450217635509901e-7)=> -4

-4.450217635509901e-7 is nothing but -0.000000450217635509901 so it should return 0 but for me its returning -4.

How to parse the values having exponential?

3
  • 5
    Are you looking for Math.floor or Math.ceil or Math.round? It doesn't seem like parseInt is what you need here, as you already have a number. Commented Sep 3, 2014 at 5:57
  • ParseInt discards decimal values Commented Sep 3, 2014 at 5:58
  • There is no exponent to parse, since the number literal will already be evaluated when parseInt is called. All it gets is a number value. parseInt is intended to be used with strings. Please explain what you are trying to achieve. What happens in your case is that the number is converted to a string, and it will be formatted using scientific notation (basically the same notation you used for the number literal). parseInt iterates over every character, collecting those it understands (leading +/- and digits). . is neither so it stops and converts the collected chars to a number. Commented Sep 3, 2014 at 6:41

1 Answer 1

2

parseInt parses a string as an integer. You are giving it a float (I don't even know why that is working). If you want to round it, use your choice of rounding methods from the Math object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.