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

Provide a binding to the .toFixed API #417

Closed
wants to merge 4 commits into
base: master
from

Conversation

Projects
None yet
4 participants
@agrafix

agrafix commented Oct 3, 2015

Convert a float into a string including a fixed number of decimal places. I think this is a very frequently used function when building user-interfaces so it might be good to but this in the core libraries.

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

rgrempel Oct 3, 2015

Contributor

According to the Mozilla documentation of toFixed, it is possible for Javascript's toFixed to throw exceptions. I suppose one wouldn't have to worry about the TypeError, since the Elm type checker will save us from that. However, there is the RangeError to consider.

So, to guarantee no runtime errors, one would have to make the return type something like a Result String String instead of just returning a string. Here's a version of the native code that returns a Result:

https://github.com/rgrempel/elm-web-api/blob/master/src/Native/WebAPI/Number.js#L31-L37

and what the interface might look like:

https://github.com/rgrempel/elm-web-api/blob/master/src/WebAPI/Number.elm#L75-L81

Or, to put it another way, you should consider this test case:

https://github.com/rgrempel/elm-web-api/blob/master/test/src/WebAPI/NumberTest.elm#L44

Contributor

rgrempel commented Oct 3, 2015

According to the Mozilla documentation of toFixed, it is possible for Javascript's toFixed to throw exceptions. I suppose one wouldn't have to worry about the TypeError, since the Elm type checker will save us from that. However, there is the RangeError to consider.

So, to guarantee no runtime errors, one would have to make the return type something like a Result String String instead of just returning a string. Here's a version of the native code that returns a Result:

https://github.com/rgrempel/elm-web-api/blob/master/src/Native/WebAPI/Number.js#L31-L37

and what the interface might look like:

https://github.com/rgrempel/elm-web-api/blob/master/src/WebAPI/Number.elm#L75-L81

Or, to put it another way, you should consider this test case:

https://github.com/rgrempel/elm-web-api/blob/master/test/src/WebAPI/NumberTest.elm#L44

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

rgrempel Oct 5, 2015

Contributor

I guess the other option, if you wanted to return a simple String instead of a Result, would be to clamp the number of digits between 0 and 20 -- at least, the Mozilla documentation suggests that an exception will not be thrown in those cases.

Contributor

rgrempel commented Oct 5, 2015

I guess the other option, if you wanted to return a simple String instead of a Result, would be to clamp the number of digits between 0 and 20 -- at least, the Mozilla documentation suggests that an exception will not be thrown in those cases.

@evancz evancz referenced this pull request Oct 10, 2015

Open

API Ideas #322

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Sep 22, 2016

Member

This is added to #322 and is easy enough to add. Makes sense to make the decision before writing the code.

Member

evancz commented Sep 22, 2016

This is added to #322 and is easy enough to add. Makes sense to make the decision before writing the code.

@evancz evancz closed this Sep 22, 2016

@witoldsz

This comment has been minimized.

Show comment
Hide comment
@witoldsz

witoldsz Apr 21, 2017

@evancz toFixed is a really nice to have. I need to format data in a table and seeing this task as closed and referenced in #721 aka "to be done in the future" makes me sad again - yesterday I was in trouble, found the solution: #842 but it's also postponed for the same reason.

witoldsz commented Apr 21, 2017

@evancz toFixed is a really nice to have. I need to format data in a table and seeing this task as closed and referenced in #721 aka "to be done in the future" makes me sad again - yesterday I was in trouble, found the solution: #842 but it's also postponed for the same reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment