@utiliread/primitives 1.0.0-46 → 1.0.0-47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -4,6 +4,8 @@ import { NumberFormatter, Parts, localeDecimalSeparator, invariantDecimalSeparat
|
|
|
4
4
|
describe("toParts and toPrecision", () => {
|
|
5
5
|
it("should format correctly", () => {
|
|
6
6
|
const data: [number, number, Parts][] = [
|
|
7
|
+
[0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
8
|
+
[0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
|
|
7
9
|
[77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
|
|
8
10
|
[77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
|
|
9
11
|
[77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
|
|
@@ -68,6 +70,8 @@ describe("toParts and toPrecision", () => {
|
|
|
68
70
|
describe("toFixed", () => {
|
|
69
71
|
it("should format correctly", () => {
|
|
70
72
|
const data: [number, number, string][] = [
|
|
73
|
+
[0, 0, "0"],
|
|
74
|
+
[0.0, 1, "0.0"],
|
|
71
75
|
[10, 1, "10"],
|
|
72
76
|
[10, 2, "10"],
|
|
73
77
|
[10, 3, "10.0"],
|
package/src/number-formatter.ts
CHANGED
|
@@ -10,6 +10,16 @@ export interface Parts {
|
|
|
10
10
|
|
|
11
11
|
class Formatter {
|
|
12
12
|
public toPrecision(value: number, precision: number) {
|
|
13
|
+
if (value === 0) {
|
|
14
|
+
// (0).toPrecision(1) == "0"
|
|
15
|
+
// (0).toPrecision(2) == "0.0"
|
|
16
|
+
// (0).toPrecision(3) == "0.00"
|
|
17
|
+
// However, we want:
|
|
18
|
+
// Formatter.toPrecision(0, 0) == "0"
|
|
19
|
+
// Formatter.toPrecision(0, 1) == "0.0"
|
|
20
|
+
// Formatter.toPrecision(0, 2) == "0.00"
|
|
21
|
+
return value.toPrecision(precision + 1);
|
|
22
|
+
}
|
|
13
23
|
const absValue = Math.abs(value);
|
|
14
24
|
if (absValue < 1) {
|
|
15
25
|
return value.toPrecision(precision);
|