-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use arguments in Number.toLocaleString (#1619)
* Matching most widely use cases for internationalization and tests. * Commenting U+2009 test that fails.
- Loading branch information
1 parent
197ebd8
commit 0a58535
Showing
3 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Ideally, internacionalization formats implemented through the ECMAScript standards would follow this: | ||
// https://tc39.es/ecma402/#sec-initializedatetimeformat | ||
// https://tc39.es/ecma402/#sec-canonicalizelocalelist | ||
// Along with the implementations of whatever is subsequenlty called. | ||
|
||
// As this is not in place (See TODOS in NumberFormatConstructor and DateTimeFormatConstructor) we can arrange | ||
// values that will match the JS behavior using the host logic. This bypasses the ECMAScript standards but can | ||
// do the job for the most common use cases and cultures meanwhile. | ||
|
||
namespace Jint.Native.Number | ||
{ | ||
internal class NumberIntlHelper | ||
{ | ||
// Obtined empirically. For all cultures tested, we get a maximum of 3 decimal digits. | ||
private const int JS_MAX_DECIMAL_DIGIT_COUNT = 3; | ||
|
||
/// <summary> | ||
/// Checks the powers of 10 of number to count the number of decimal digits. | ||
/// Returns a clamped JS_MAX_DECIMAL_DIGIT_COUNT count. | ||
/// JavaScript will use the shortest representation that accurately represents the value | ||
/// and clamp the decimal digits to JS_MAX_DECIMAL_DIGIT_COUNT. | ||
/// C# fills the digits with zeros up to the culture's numberFormat.NumberDecimalDigits | ||
/// and does not provide the same max (numberFormat.NumberDecimalDigits != JS_MAX_DECIMAL_DIGIT_COUNT). | ||
/// This function matches the JS behaviour for the decimal digits returned, this is the actual decimal | ||
/// digits for a number (with no zeros fill) clamped to JS_MAX_DECIMAL_DIGIT_COUNT. | ||
/// </summary> | ||
public static int GetDecimalDigitCount(double number) | ||
{ | ||
for (int i = 0; i < JS_MAX_DECIMAL_DIGIT_COUNT; i++) | ||
{ | ||
var powOf10 = number * System.Math.Pow(10, i); | ||
bool isInteger = powOf10 == ((int) powOf10); | ||
if (isInteger) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return JS_MAX_DECIMAL_DIGIT_COUNT; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters