-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for Float #5
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
base: main
Are you sure you want to change the base?
Conversation
src/JSON.mo
Outdated
|
|
||
| public func show(json : JSON) : Text = switch (json) { | ||
| case (#Number(v)) { Int.toText(v); }; | ||
| case (#Float(v)) { Float.format(#fix 2, v); }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not lose precision in the textual representation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the code so it returns the exact format.
|
Also, I tried to implement the number fraction exponent in this #3, but I think there will be some confusion because motoko only recognizes the format as a |
q-uint
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tomijaga, thanks for the changes!
I would like to keep the dependencies to a minimum, so the best option might be to revert to the original truncated floats...
test/Show.mo
Outdated
| assert (JSON.show(#Float(-3.14)) == "-3.1400000000000001"); | ||
| assert (JSON.show(#Float(1.234e-4)) == "0.00012339999999999999"); | ||
| assert (JSON.show(#Float(43e-02)) == "0.42999999999999999"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Motoko does not seem to work well with precision.
q-uint
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mention that a Float only has two decimals somewhere in the README?

Additions
#Floatto theJSONvariantFloatshow()andparse()