-
Notifications
You must be signed in to change notification settings - Fork 18
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
toString() and normalization #12
Comments
Maybe add an argument to toString much like Number's toString? 1.0n..toString(4) gives us 4 decimal places. 1.0n..toString(0) returns no decimal places. |
For Number literals |
I’m confused; in this proposal, 5m and 5.00m are not the same identical number? |
As I understand |
Different types i understand; precision to me doesn’t seem like it should be part of identity. |
@ljharb This is an interesting point; maybe you could file another issue if you want to pursue this further. This issue is about working out the semantics for toString under the assumption that BigDecimal would support trailing zeroes. As the README points out, trailing zeroes are a feature of many Decimal types, and they are important for many applications. See background in http://speleotrove.com/decimal/decifaq1.html#tzeros . |
I'll try to find time to file another issue for that; as for this one, if |
It's always possible to state more invariants. I'd like to hear your rationale. |
Java's BigDecimal has special method for comparison |
Let's continue the comparison discussion in #11. As mentioned in that issue, I'd like to use @waldemarhorwat's proposal as a starting point for both |
My rationale is that the string representation of two different things should be different (if the toString output is going to be useful at all). If 5.0d and 5.00d are the same (===) then they should have the same string output; if different, different. |
Waldemar's proposal was that the two BigDecimals |
No, my proposal was not that that example (or, for even more fun, |
@waldemarhorwat Thanks for correcting my misunderstanding of those past email threads. The current semantics in the README is that they would be the same, indistinguishable, normalized. |
For background, if you were to discriminate IEEE decimal cohort members such as |
This issue hasn't been updated in almost 5 years but I wanted to weigh in here. My understanding is that the current state of the proposal uses a "hybrid" normalization strategy where the data model retains Decimal128 but comparison and toString use normalized values by default. At the same time, Intl.NumberFormat retains the trailing zeros for reasons I've discussed on the record at multiple TC39 meetings (and am willing to write up again here upon request). This introduces a potential inconsistency: toString and toLocaleString may print the values at different precisions. Do we want to make toLocaleString more consistent with toString or more consistent with Intl.NF.p.format? Note that Temporal is considering introducing some cases where toLocaleString passes some arguments (such as the calendar) into Intl.DTF that otherwise aren't available, so I think it's not too bad if Decimal chose to do something similar. CC @jessealama |
(Sorry this got dropped.) IMO a disagreement between
I'd lean toward being more consistent with It seems to me the decimal integration is a bit simpler than the Temporal integration in that it should be possible to pass the full underlying decimal data to |
I can buy into the comparison operations working on normalized values, but I think it's worth keeping open the discussion of whether |
EDIT: I seem to have misunderstood Waldemar's proposal; see #12 (comment) for clarification.
In this post, @waldemarhorwat proposed that
BigDecimal.prototype.toString()
(and presumably ToString applied to BigDecimal) should cut off all trailing zeroes, and not distinguish between elements of the same "cohort" (that is, decimals that have the same mathematical value, but different numbers of significant figures/trailing 0's).One piece of motivation for Waldmar's argument is that it could be seen as surprising if
arr[5m]
was not the same asarr[5.000m]
. But personally, I find it a little surprising if toString always discards the trailing zeros that we'd otherwise work hard to preserve. It may be semantically wrong for end users iftoString()
is used on BigDecimal and it cuts off trailing zeros unexpectedly.I see a few options that we might take to square this circle:
BigDecimal.prototype.toStringNormalized()
andBigDecimal.prototype.toStringWithTrailingZeroes()
, and not have atoString()
method.showTrailingZeroes
option, and if it's not provided, throw.toString()
, while discouraging developers from using BigDecimal as an array index.Thoughts?
The text was updated successfully, but these errors were encountered: