notation-engineering.js (6395B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 3 const { 4 Nan, Inf, Integer, MinusSign, PlusSign, Decimal, Fraction, Group, 5 ExponentSeparator, ExponentInteger, ExponentMinusSign, 6 } = NumberFormatParts; 7 8 const testcases = [ 9 { 10 locale: "en", 11 options: { 12 notation: "engineering", 13 }, 14 values: [ 15 {value: +0, string: "0E0", parts: [Integer("0"), ExponentSeparator("E"), ExponentInteger("0")]}, 16 {value: -0, string: "-0E0", parts: [MinusSign("-"), Integer("0"), ExponentSeparator("E"), ExponentInteger("0")]}, 17 {value: 0n, string: "0E0", parts: [Integer("0"), ExponentSeparator("E"), ExponentInteger("0")]}, 18 19 {value: 1, string: "1E0", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("0")]}, 20 {value: 10, string: "10E0", parts: [Integer("10"), ExponentSeparator("E"), ExponentInteger("0")]}, 21 {value: 100, string: "100E0", parts: [Integer("100"), ExponentSeparator("E"), ExponentInteger("0")]}, 22 {value: 1000, string: "1E3", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("3")]}, 23 {value: 10000, string: "10E3", parts: [Integer("10"), ExponentSeparator("E"), ExponentInteger("3")]}, 24 {value: 100000, string: "100E3", parts: [Integer("100"), ExponentSeparator("E"), ExponentInteger("3")]}, 25 {value: 1000000, string: "1E6", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("6")]}, 26 27 {value: 1n, string: "1E0", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("0")]}, 28 {value: 10n, string: "10E0", parts: [Integer("10"), ExponentSeparator("E"), ExponentInteger("0")]}, 29 {value: 100n, string: "100E0", parts: [Integer("100"), ExponentSeparator("E"), ExponentInteger("0")]}, 30 {value: 1000n, string: "1E3", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("3")]}, 31 {value: 10000n, string: "10E3", parts: [Integer("10"), ExponentSeparator("E"), ExponentInteger("3")]}, 32 {value: 100000n, string: "100E3", parts: [Integer("100"), ExponentSeparator("E"), ExponentInteger("3")]}, 33 {value: 1000000n, string: "1E6", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("6")]}, 34 35 {value: 0.1, string: "100E-3", parts: [Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3")]}, 36 {value: 0.01, string: "10E-3", parts: [Integer("10"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3")]}, 37 {value: 0.001, string: "1E-3", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3")]}, 38 {value: 0.0001, string: "100E-6", parts: [Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("6")]}, 39 {value: 0.00001, string: "10E-6", parts: [Integer("10"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("6")]}, 40 {value: 0.000001, string: "1E-6", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("6")]}, 41 {value: 0.0000001, string: "100E-9", parts: [Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("9")]}, 42 43 {value: Infinity, string: "∞", parts: [Inf("∞")]}, 44 {value: -Infinity, string: "-∞", parts: [MinusSign("-"), Inf("∞")]}, 45 46 {value: NaN, string: "NaN", parts: [Nan("NaN")]}, 47 {value: -NaN, string: "NaN", parts: [Nan("NaN")]}, 48 ], 49 }, 50 51 // Exponent modifications take place early, so while in the "standard" notation 52 // `Intl.NumberFormat("en", {maximumFractionDigits: 0}).format(0.1)` returns "0", for 53 // "engineering" notation the result string is not "0", but instead "100E-3". 54 55 { 56 locale: "en", 57 options: { 58 notation: "engineering", 59 maximumFractionDigits: 0, 60 }, 61 values: [ 62 {value: 0.1, string: "100E-3", parts: [ 63 Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3") 64 ]}, 65 ], 66 }, 67 68 { 69 locale: "en", 70 options: { 71 notation: "engineering", 72 minimumFractionDigits: 4, 73 }, 74 values: [ 75 {value: 10, string: "10.0000E0", parts: [ 76 Integer("10"), Decimal("."), Fraction("0000"), ExponentSeparator("E"), ExponentInteger("0") 77 ]}, 78 {value: 0.1, string: "100.0000E-3", parts: [ 79 Integer("100"), Decimal("."), Fraction("0000"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3") 80 ]}, 81 ], 82 }, 83 84 { 85 locale: "en", 86 options: { 87 notation: "engineering", 88 minimumIntegerDigits: 4, 89 }, 90 values: [ 91 {value: 10, string: "0,010E0", parts: [ 92 Integer("0"), Group(","), Integer("010"), ExponentSeparator("E"), ExponentInteger("0") 93 ]}, 94 {value: 0.1, string: "0,100E-3", parts: [ 95 Integer("0"), Group(","), Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3") 96 ]}, 97 ], 98 }, 99 100 { 101 locale: "en", 102 options: { 103 notation: "engineering", 104 minimumSignificantDigits: 4, 105 }, 106 values: [ 107 {value: 10, string: "10.00E0", parts: [ 108 Integer("10"), Decimal("."), Fraction("00"), ExponentSeparator("E"), ExponentInteger("0") 109 ]}, 110 {value: 0.1, string: "100.0E-3", parts: [ 111 Integer("100"), Decimal("."), Fraction("0"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3") 112 ]}, 113 ], 114 }, 115 116 { 117 locale: "en", 118 options: { 119 notation: "engineering", 120 maximumSignificantDigits: 1, 121 }, 122 values: [ 123 {value: 12, string: "10E0", parts: [ 124 Integer("10"), ExponentSeparator("E"), ExponentInteger("0") 125 ]}, 126 {value: 0.12, string: "100E-3", parts: [ 127 Integer("100"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3") 128 ]}, 129 ], 130 }, 131 ]; 132 133 runNumberFormattingTestcases(testcases); 134 135 if (typeof reportCompare === "function") 136 reportCompare(true, true);