-
Notifications
You must be signed in to change notification settings - Fork 106
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
BigInt.prototype.toLocaleString #218
Comments
ICU4C has |
That looks perfect for this case. Seems like we could pass the output of ToString(bigInt) to @anba Would you be interested in writing specification text for BigInt.prototype.toLocaleString in ECMA 402? The design I was imagining would be to base it on an overload of Intl.NumberFormat.prototype.format for BigInts--Number Format Functions could start with ToNumeric rather than ToNumber, and then PartitionNumberPattern would somehow be either generalized or cloned to handle BigInt in addition to Number. cc @cxielarko |
AFAICT the descriptions for
And maybe Do we also want to modify (or add a new method) to allow BigInt values for |
FYI, better than numberformatter.h has an endpoint for the same syntax as FormattedNumber result = NumberFormatter::withLocale(...).formatDecimal(..., status);
result.populateFieldPositionIterator(...);
UnicodeString resultString = result.toString(); |
Thanks for the info that the new number formatting API will support this use case! For SpiderMonkey we may need to wait for https://ssl.icu-project.org/trac/ticket/13597, because we generally only use the C-API for its compatibility across different releases. |
As a general comment, I think that we should interpret string inputs as the highest precision datatype supported. So if I give a string containing something like "987654321987654321987654321", that should be interpreted as a BigInteger. However, "9876.543" should be interpreted as a double until Ecma adds a datatype for BigDecimal, for example. |
I'd prefer not to do this sort of "magical" semantic overloading. If your intuition is that that should exist, then I'm inclined to add another method, and avoid overloading, e.g., Intl.NumberFormat.prototype.formatBigInt/formatBigIntToParts. What would you think of that? |
I don't see this as "magical" overloading. I see it as the method taking the input data type and then internally choosing the highest precision data type available to represent it. My intuition is that it's weird to ever convert strings to Numbers, and we should avoid it when possible with better alternative behavior. |
Following this intuition, I'd like to mentally classify the |
This patch brings Intl.NumberFormat support to BigInt, and adds a BigInt.prototype.toLocaleString method based on it. The design here is to include overloading between BigInt and Number as arguments for the format and formatToParts methods based on ToNumeric. This means that, for example, string arguments are cast to Number, rather than BigInt. This design preserves compatibility and consistency with operators like unary - This definition permits options in the NumberFormat to force decimal places, e.g., 1n formatting as 1.00000 if the minimum fractional digits is 5. Alternative semantics would be to throw an exception in this case. For the algorithm text itself: the specification algorithms ToRawPrecision and ToRawFixed are now used for both Numbers and BigInts. Given the ECMAScript specification's use of implicit coercisions between Numbers and mathematical values, I believe that this is valid without any special changes; the phrasing may change in the future [1]. ICU4C-based implementations of ECMAScript can use LocalizedNumberFormatter::formatDecimal [2] or unum_formatDecimal [3] to implement the algorithms in this patch. [1] tc39/ecma262#1135 [2] http://icu-project.org/apiref/icu4c/classicu_1_1number_1_1LocalizedNumberFormatter.html#a29cd3d107b784496e19175ce0115f26f [3] http://icu-project.org/apiref/icu4c/unum_8h.html#a59870a322f012dc1b9d99cf8a7b708f1 Closes tc39#218
Could someone show me a js example code of how to use BigInt with the toLocaleString? IS it like 12345678901234567890n.toLocaleString("fr") ? |
Yes, that looks correct. |
Thanks for the code review from @jakobkummerow the v8 implementation sync with the current spec ( 040f809 ) is landed into v8 tree behind the flag --harmony-intl-bigint . You can also see the test I wrote here |
Great work, @FrankYFTang ! That's right; for BigInt, I'm going to present on this issue in the upcoming TC39 meeting, arguing we should instead make a separate method, |
I can understand there being a difference between |
The reason my current v8 implementation output BigInt(-0) to "0" is because the BitInt.prototype.toString output "0" instead of "-0". |
This patch brings Intl.NumberFormat support to BigInt, and adds a BigInt.prototype.toLocaleString method based on it. The design here is to include overloading between BigInt and Number as arguments for the format and formatToParts methods based on ToNumeric. This means that, for example, string arguments are cast to Number, rather than BigInt. This design preserves compatibility and consistency with operators like unary - This definition permits options in the NumberFormat to force decimal places, e.g., 1n formatting as 1.00000 if the minimum fractional digits is 5. Alternative semantics would be to throw an exception in this case. For the algorithm text itself: the specification algorithms ToRawPrecision and ToRawFixed are now used for both Numbers and BigInts. Given the ECMAScript specification's use of implicit coercisions between Numbers and mathematical values, I believe that this is valid without any special changes; the phrasing may change in the future [1]. ICU4C-based implementations of ECMAScript can use LocalizedNumberFormatter::formatDecimal [2] or unum_formatDecimal [3] to implement the algorithms in this patch. [1] tc39/ecma262#1135 [2] http://icu-project.org/apiref/icu4c/classicu_1_1number_1_1LocalizedNumberFormatter.html#a29cd3d107b784496e19175ce0115f26f [3] http://icu-project.org/apiref/icu4c/unum_8h.html#a59870a322f012dc1b9d99cf8a7b708f1 Closes tc39#218
And the reason that See our implementation of BigInt negation:
|
This patch brings Intl.NumberFormat support to BigInt, and adds a BigInt.prototype.toLocaleString method based on it. The design here is to include overloading between BigInt and Number as arguments for the format and formatToParts methods based on ToNumeric. This means that, for example, string arguments are cast to Number, rather than BigInt. This design preserves compatibility and consistency with operators like unary - This definition permits options in the NumberFormat to force decimal places, e.g., 1n formatting as 1.00000 if the minimum fractional digits is 5. Alternative semantics would be to throw an exception in this case. For the algorithm text itself: the specification algorithms ToRawPrecision and ToRawFixed are now used for both Numbers and BigInts. Given the ECMAScript specification's use of implicit coercisions between Numbers and mathematical values, I believe that this is valid without any special changes; the phrasing may change in the future [1]. ICU4C-based implementations of ECMAScript can use LocalizedNumberFormatter::formatDecimal [2] or unum_formatDecimal [3] to implement the algorithms in this patch. [1] tc39/ecma262#1135 [2] http://icu-project.org/apiref/icu4c/classicu_1_1number_1_1LocalizedNumberFormatter.html#a29cd3d107b784496e19175ce0115f26f [3] http://icu-project.org/apiref/icu4c/unum_8h.html#a59870a322f012dc1b9d99cf8a7b708f1 Closes #218
In the Stage 3 BigInt proposal, toLocaleString is defined. We should have a definition for this in the ECMA-402 specification.
I wasn't able to find an ICU API which would be useful here; does one exist?
cc @jakobkummerow
The text was updated successfully, but these errors were encountered: