-
Notifications
You must be signed in to change notification settings - Fork 20
Description
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 as arr[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 if toString() is used on BigDecimal and it cuts off trailing zeros unexpectedly.
I see a few options that we might take to square this circle:
- Just tough it out and teach people to use Intl.NumberFormat when they want trailing zeroes represented (you should probably be doing something locale-specific when displaying numbers to people anyway).
- Require that the option of whether or not to display trailing zeroes is provided to the toString algorithm, and make the ToString algorithm simply throw on BigDecimal, either through
- Having two separate methods,
BigDecimal.prototype.toStringNormalized()andBigDecimal.prototype.toStringWithTrailingZeroes(), and not have atoString()method. - Make the second parameter an options bag that's required to have a
showTrailingZeroesoption, and if it's not provided, throw.
- Having two separate methods,
- Tough it out the other way and just include trailing zeroes in
toString(), while discouraging developers from using BigDecimal as an array index.
Thoughts?