-
Notifications
You must be signed in to change notification settings - Fork 2
/
math.ts
36 lines (33 loc) · 971 Bytes
/
math.ts
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
export function clamp(value: number, a: number, b: number) {
const min = Math.min(a, b)
const max = Math.max(a, b)
return Math.min(Math.max(value, min), max)
}
// write a function that does the same as "number.toFixed(n)"
// but omit the trailing zeros
export function toFixed(value: number, n = 1) {
const str = value.toFixed(n)
if (str.indexOf('.') !== -1) {
return str.replace(/\.?0+$/, '')
} else {
return str
}
}
export function prettifyNumber(n: number, inChinese = false): string {
if (inChinese) {
if (Math.abs(n) >= 100000000) {
return toFixed(n / 100000000) + '亿'
} else if (Math.abs(n) >= 10000) {
return toFixed(n / 10000) + '万'
} else {
return Intl.NumberFormat('en-US').format(n)
}
}
if (Math.abs(n) >= 1000000) {
return toFixed(n / 1000000) + 'm'
} else if (Math.abs(n) >= 1000) {
return toFixed(n / 1000) + 'k'
} else {
return Intl.NumberFormat('en-US').format(n)
}
}