Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const NICE_FRACTIONS = new Map<number, string>(
[
[0, ""],
[10, "⅒"],
[11, "⅑"],
[12, "⅛"],
[14, "⅐"],
[16, "⅙"],
[20, "⅕"],
[25, "¼"],
[33, "⅓"],
[37, "⅜"],
[40, "⅖"],
[50, "½"],
[60, "⅗"],
[62, "⅝"],
[66, "⅔"],
[75, "¾"],
[80, "⅘"],
[83, "⅚"],
[87, "⅞"],
],
);
// TODO: refactor
export function formatFloat(a: number) {
const intPart = Math.floor(a);
const remainder = Math.floor((a - intPart) * 100);
const nice = NICE_FRACTIONS.get(remainder);
if (nice !== undefined) {
return `${intPart === 0 ? "" : intPart}${nice}`;
}
return a.toFixed(2);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
November 16, 2023