-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumber.ts
More file actions
26 lines (20 loc) · 818 Bytes
/
number.ts
File metadata and controls
26 lines (20 loc) · 818 Bytes
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
export function convert(num: any, digits = 0) {
if (num === null || num === undefined || isNaN(num)) return undefined;
const res = digits === 0 ? parseInt(num) : parseFloat(parseFloat(num + '').toFixed(digits));
if (isNaN(res)) return undefined;
return res;
}
export function chance(probability: number) {
return Math.random() < probability / 100;
}
export function random(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Method to format a number as a string with a sign
export function format(num: number): string {
return num >= 0 ? '+' + num : '-' + num;
}
// Assume normalizeFloat is defined elsewhere in the class
export function normalizeFloat(value: number, precision: number = 2): number {
return parseFloat(value.toFixed(precision));
}