Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseInt can also operate on numbers. #15122

Closed
wants to merge 1 commit into from
Closed

Conversation

endel
Copy link

@endel endel commented Apr 11, 2017

Not sure if this has been reported already. I've searched in the closed issues and pull requests and couldn't find anything related.

parseInt(3.5) is a valid call on JavaScript and has a reasonable return value.

Example:

var rounded = parseInt(3.5);
console.log(rounded);

Expected behaviour:

Output 3 in the console.

Actual behaviour:

Argument of type 'number' is not assignable to parameter of type 'string'.

You can see there's also some examples that provides numbers to parseInt on Mozilla's documentation: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Examples

`parseInt(3.5)` is a valid call on JavaScript and has a reasonable return value.
@msftclas
Copy link

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. We will now review your pull request.
Thanks,
Microsoft Pull Request Bot

@sandersn
Copy link
Member

Makes sense to me. @rbuckton what is your opinion?

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Apr 11, 2017

Absolutely not! Don't use parseInt as a poor man's Math.floor!

parseInt(8e20)
> 800000000000000000000
parseInt(8e21)
> 8
parseInt(2e-7)
> 2
parseInt(2e-6)
> 0

@rbuckton
Copy link
Member

I concur with @RyanCavanaugh, the MDN docs are promoting bad behavior in that example. Use Math.floor, Math.round, or n | 0 if you want to get an integer from a double.

@RyanCavanaugh
Copy link
Member

I've posted an edit to the MDN page

@endel endel deleted the patch-1 branch April 12, 2017 06:17
@endel
Copy link
Author

endel commented Apr 12, 2017

Thanks for the clarification and feedback, guys. I really appreciate that.

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants