Skip to content

Commit

Permalink
Expand SI units to include everything from quecto to quetta
Browse files Browse the repository at this point in the history
ref #403
  • Loading branch information
frostburn committed Nov 26, 2023
1 parent ebf7f64 commit b28037a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__tests__/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ describe("Hertz formatter", () => {
it("supports megahertz", () => {
expect(formatHertz(123.456e6)).toBe("123.456MHz");
});

it("supports ronnahertz", () => {
expect(formatHertz(123.456e27)).toBe("123.456RHz");
});

it("falls back to exponential format for unreasonably large frequencies", () => {
expect(formatHertz(1.23456e40)).toBe("1.235e+40Hz");
});

it("zeros out with unreasonably small frequencies", () => {
expect(formatHertz(1.23456e-40)).toBe("0.000qHz");
});
});

describe("Auto key color algorithm", () => {
Expand Down
14 changes: 13 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export function formatHertz(frequency: number, fractionDigits = 3) {
}

// Submultiples
if (magnitude < 1e-27) {
return (frequency * 1e30).toFixed(fractionDigits) + "qHz";
}
if (magnitude < 1e-24) {
return (frequency * 1e27).toFixed(fractionDigits) + "rHz";
}
if (magnitude < 1e-21) {
return (frequency * 1e24).toFixed(fractionDigits) + "yHz";
}
Expand All @@ -101,11 +107,17 @@ export function formatHertz(frequency: number, fractionDigits = 3) {
}

// Too large. Use scientific notation.
if (magnitude > 1e28) {
if (magnitude > 1e34) {
return formatExponential(frequency) + "Hz";
}

// Multiples
if (magnitude > 1e30) {
return (frequency * 1e-30).toFixed(fractionDigits) + "QHz";
}
if (magnitude > 1e27) {
return (frequency * 1e-27).toFixed(fractionDigits) + "RHz";
}
if (magnitude > 1e24) {
return (frequency * 1e-24).toFixed(fractionDigits) + "YHz";
}
Expand Down

0 comments on commit b28037a

Please sign in to comment.