Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upProvide a binding to the .toFixed API #417
Conversation
agrafix
added some commits
Oct 3, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
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
|
According to the Mozilla documentation of So, to guarantee no runtime errors, one would have to make the return type something like a 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 |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
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.
|
I guess the other option, if you wanted to return a simple |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
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.
|
This is added to #322 and is easy enough to add. Makes sense to make the decision before writing the code. |
agrafix commentedOct 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.