MCPcopy
hub / github.com/formatjs/formatjs / ToRawFixed

Function ToRawFixed

packages/ecma402-abstract/NumberFormat/ToRawFixed.ts:43–141  ·  view source on GitHub ↗
(
  x: Decimal,
  minFraction: number,
  maxFraction: number,
  roundingIncrement: number,
  unsignedRoundingMode: UnsignedRoundingModeType
)

Source from the content-addressed store, hash-verified

41 * @param maxFraction an integer between 0 and 20
42 */
43export function ToRawFixed(
44 x: Decimal,
45 minFraction: number,
46 maxFraction: number,
47 roundingIncrement: number,
48 unsignedRoundingMode: UnsignedRoundingModeType
49): RawNumberFormatResult {
50 // 1. Let f be maxFraction.
51 const f = maxFraction
52
53 // 2. Let n1 and r1 be the results of performing the maximized rounding of x to f fraction digits.
54 const {n1, r1} = findN1R1(x, f, roundingIncrement)
55
56 // 3. Let n2 and r2 be the results of performing the minimized rounding of x to f fraction digits.
57 const {n2, r2} = findN2R2(x, f, roundingIncrement)
58
59 // 4. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
60 const r = ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode)
61
62 let n: Decimal, xFinal: Decimal
63 let m: string
64
65 // 5. If r is equal to r1, then
66 if (r.eq(r1)) {
67 // a. Let n be n1.
68 n = n1
69 // b. Let xFinal be r1.
70 xFinal = r1
71 } else {
72 // 6. Else,
73 // a. Let n be n2.
74 n = n2
75 // b. Let xFinal be r2.
76 xFinal = r2
77 }
78
79 // 7. If n is 0, let m be "0".
80 if (n.isZero()) {
81 m = '0'
82 } else {
83 // 8. Else, let m be the String representation of n.
84 m = n.toString()
85 }
86
87 let int
88
89 // 9. If f is not 0, then
90 if (f !== 0) {
91 // a. Let k be the length of m.
92 let k = m.length
93
94 // b. If k < f, then
95 if (k <= f) {
96 // i. Let z be the String value consisting of f + 1 - k occurrences of the character "0".
97 const z = repeat('0', f - k + 1)
98 // ii. Set m to the string-concatenation of z and m.
99 m = z + m
100 // iii. Set k to f + 1.

Callers 3

FormatNumericToStringFunction · 0.85
partitionNumberIntoPartsFunction · 0.85

Calls 7

findN1R1Function · 0.85
findN2R2Function · 0.85
repeatFunction · 0.85
eqMethod · 0.80
isZeroMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected