notation-scientific.js (6330B)
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: "scientific", 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: "1E1", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("1")]}, 21 {value: 100, string: "1E2", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("2")]}, 22 {value: 1000, string: "1E3", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("3")]}, 23 {value: 10000, string: "1E4", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("4")]}, 24 {value: 100000, string: "1E5", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("5")]}, 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: "1E1", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("1")]}, 29 {value: 100n, string: "1E2", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("2")]}, 30 {value: 1000n, string: "1E3", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("3")]}, 31 {value: 10000n, string: "1E4", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("4")]}, 32 {value: 100000n, string: "1E5", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("5")]}, 33 {value: 1000000n, string: "1E6", parts: [Integer("1"), ExponentSeparator("E"), ExponentInteger("6")]}, 34 35 {value: 0.1, string: "1E-1", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1")]}, 36 {value: 0.01, string: "1E-2", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("2")]}, 37 {value: 0.001, string: "1E-3", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("3")]}, 38 {value: 0.0001, string: "1E-4", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("4")]}, 39 {value: 0.00001, string: "1E-5", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("5")]}, 40 {value: 0.000001, string: "1E-6", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("6")]}, 41 {value: 0.0000001, string: "1E-7", parts: [Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("7")]}, 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 // "scientific" notation the result string is not "0", but instead "1E-1". 54 55 { 56 locale: "en", 57 options: { 58 notation: "scientific", 59 maximumFractionDigits: 0, 60 }, 61 values: [ 62 {value: 0.1, string: "1E-1", parts: [ 63 Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1") 64 ]}, 65 ], 66 }, 67 68 { 69 locale: "en", 70 options: { 71 notation: "scientific", 72 minimumFractionDigits: 4, 73 }, 74 values: [ 75 {value: 10, string: "1.0000E1", parts: [ 76 Integer("1"), Decimal("."), Fraction("0000"), ExponentSeparator("E"), ExponentInteger("1") 77 ]}, 78 {value: 0.1, string: "1.0000E-1", parts: [ 79 Integer("1"), Decimal("."), Fraction("0000"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1") 80 ]}, 81 ], 82 }, 83 84 { 85 locale: "en", 86 options: { 87 notation: "scientific", 88 minimumIntegerDigits: 4, 89 }, 90 values: [ 91 {value: 10, string: "0,001E1", parts: [ 92 Integer("0"), Group(","), Integer("001"), ExponentSeparator("E"), ExponentInteger("1") 93 ]}, 94 {value: 0.1, string: "0,001E-1", parts: [ 95 Integer("0"), Group(","), Integer("001"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1") 96 ]}, 97 ], 98 }, 99 100 { 101 locale: "en", 102 options: { 103 notation: "scientific", 104 minimumSignificantDigits: 4, 105 }, 106 values: [ 107 {value: 10, string: "1.000E1", parts: [ 108 Integer("1"), Decimal("."), Fraction("000"), ExponentSeparator("E"), ExponentInteger("1") 109 ]}, 110 {value: 0.1, string: "1.000E-1", parts: [ 111 Integer("1"), Decimal("."), Fraction("000"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1") 112 ]}, 113 ], 114 }, 115 116 { 117 locale: "en", 118 options: { 119 notation: "scientific", 120 maximumSignificantDigits: 1, 121 }, 122 values: [ 123 {value: 12, string: "1E1", parts: [ 124 Integer("1"), ExponentSeparator("E"), ExponentInteger("1") 125 ]}, 126 {value: 0.12, string: "1E-1", parts: [ 127 Integer("1"), ExponentSeparator("E"), ExponentMinusSign("-"), ExponentInteger("1") 128 ]}, 129 ], 130 }, 131 ]; 132 133 runNumberFormattingTestcases(testcases); 134 135 if (typeof reportCompare === "function") 136 reportCompare(true, true);