Skip to content

Commit ef5aada

Browse files
committed
1 parent 6a1ff02 commit ef5aada

5 files changed

Lines changed: 22 additions & 38 deletions

File tree

packages/ecma402-abstract/NumberFormat/PartitionNumberPattern.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export function PartitionNumberPattern(
1717
_x: Decimal
1818
): NumberFormatPart[] {
1919
let x = _x
20-
let stringDigitCount = (x as any).__StringDigitCount ?? 0
2120
// IMPL: We need to record the magnitude of the number
2221
let magnitude = 0
2322

@@ -49,13 +48,9 @@ export function PartitionNumberPattern(
4948
// 8.a. If x < 0, let x be -x.
5049
invariant(x.isFinite(), 'Input must be a mathematical value')
5150

52-
let magnitude_ = Number(x.abs().log(10).floor())
53-
5451
// 8.b. If internalSlots.[[style]] is "percent", let x be 100 × x.
5552
if (internalSlots.style == 'percent') {
5653
x = x.times(100)
57-
if (magnitude_ < 0) stringDigitCount += Math.max(magnitude_, -2)
58-
magnitude_ += 2
5954
}
6055

6156
// 8.c. Let exponent be ComputeExponent(numberFormat, x).
@@ -67,11 +62,8 @@ export function PartitionNumberPattern(
6762

6863
// 8.d. Let x be x × 10^(-exponent).
6964
x = x.times(getPowerOf10(-exponent))
70-
if (magnitude_ < 0 && exponent < 0) {
71-
stringDigitCount += Math.max(magnitude_, exponent)
72-
}
7365
}
74-
Object.assign(x, {__StringDigitCount: stringDigitCount})
66+
Object.assign(x, {__StringDigitCount: (_x as any).__StringDigitCount})
7567

7668
// 8.e. Let formatNumberResult be FormatNumericToString(internalSlots, x).
7769
const formatNumberResult = FormatNumericToString(internalSlots, x)

packages/ecma402-abstract/NumberFormat/ToRawFixed.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ export function ToRawFixed(
114114
let b = m.slice(m.length - f)
115115
// e. Let int be the length of a.
116116
int = a.length
117+
118+
let sfc = stringDigitCount - int
119+
const zm = (a + b).match(/^0+/)
120+
if (zm && !n.isZero()) sfc += zm[0].length
121+
117122
// f. Let cut be maxFraction - max(stringDigitCount - int, minFraction).
118-
let cut = maxFraction - Math.max(stringDigitCount - int, minFraction)
123+
let cut = maxFraction - Math.max(sfc, minFraction)
119124
// g. Repeat, while cut > 0 and the last code unit of b is 0x0030 (DIGIT ZERO),
120125
while (cut > 0 && b[b.length - 1] === '0') {
121126
// i. Remove the last code unit from b.

packages/ecma402-abstract/NumberFormat/ToRawPrecision.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,14 @@ export function ToRawPrecision(
211211

212212
// 7. If m contains ".", and maxPrecision > minPrecision, then
213213
if (m.includes('.') && maxPrecision > minPrecision) {
214+
let z = 0
215+
const zm = m.match(/^0(\.0+)?/)
216+
if (zm && !x.isZero()) {
217+
z = zm[1]?.length ?? 1
218+
}
219+
214220
// a. Let cut be maxPrecision - minPrecision.
215-
let cut = maxPrecision - Math.max(stringDigitCount, minPrecision)
221+
let cut = maxPrecision - Math.max(z + stringDigitCount, minPrecision)
216222
// b. Repeat, while cut > 0 and the last character of m is "0",
217223
while (cut > 0 && m[m.length - 1] === '0') {
218224
// i. Remove the last character from m.

packages/ecma402-abstract/ToIntlMathematicalValue.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,20 @@ export function ToIntlMathematicalValue(input: unknown): Decimal {
3232
try {
3333
const d = new Decimal(primValue as any)
3434
if (typeof primValue === 'string') {
35-
let m = 0
36-
let n = 0
3735
const numericLiteral = primValue.trim()
3836
const unsignedDecimalLiteral = numericLiteral.startsWith('-')
3937
? numericLiteral.substring(1)
4038
: numericLiteral
4139

42-
const decDotDec = unsignedDecimalLiteral.match(/^0*([0-9]+)\.([0-9]*)/)
43-
if (decDotDec) {
44-
m = decDotDec[1].length
45-
n = decDotDec[2].length
46-
} else {
47-
const dotDec = unsignedDecimalLiteral.match(/^0*\.([0-9]+)/)
48-
if (dotDec) {
49-
m = 1
50-
n = dotDec[1].length
51-
} else {
52-
const dec = unsignedDecimalLiteral.match(/^0*([0-9]+)/)
53-
if (dec) {
54-
m = dec[1].length
55-
n = 0
56-
}
57-
}
40+
let stringDigitCount = 0
41+
const match = unsignedDecimalLiteral.match(/^([0-9]*)(?:\.([0-9]*))?/)!
42+
if (match) {
43+
const fd = match[2] ?? ''
44+
stringDigitCount =
45+
(match[1] + fd).replace(/^0+/, '').length || 1 + fd.length
5846
}
5947

60-
let f = 0
61-
const exp = unsignedDecimalLiteral.match(/[eE]([+-]?[0-9]+)$/)
62-
if (exp) {
63-
const e = Number(exp[1])
64-
if (m + e < 1) m = 1 - e
65-
}
66-
67-
Object.assign(d, {__StringDigitCount: m + n + f})
48+
Object.assign(d, {__StringDigitCount: stringDigitCount})
6849
}
6950
return d
7051
} catch {

packages/intl-numberformat/tests/keep-trailing-zeros.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ assert.sameValue(nf.format('1.230e-2'), '0.01230')
4848
assert.sameValue(nf.format('12.30e-1'), '1.230')
4949
assert.sameValue(nf.format('12.30e-2'), '0.1230')
5050
assert.sameValue(nf.format('12.30e-3'), '0.01230')
51-
assert.sameValue(nf.format('1.2345e-1000'), '0.00000000000000000000')
51+
assert.sameValue(nf.format('1.2345e-1000'), '0.0000')
5252

5353
// Default maximumfractionDigits is 3
5454
const nf2 = new NumberFormat('en-US', {minimumFractionDigits: 1})

0 commit comments

Comments
 (0)