mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
Check for safe number
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { isSafeNumber } from '../type-guards.ts';
|
||||
|
||||
export function getDecimalPlaces(n: number) {
|
||||
if (!isSafeNumber(n)) return 0;
|
||||
if (n % 1 === 0) return 0;
|
||||
return n.toString().split('.')[1].length;
|
||||
}
|
||||
@@ -36,6 +39,7 @@ export function getDecimalPlaces(n: number) {
|
||||
* be used as a reference for the desired specificity.
|
||||
*/
|
||||
export function roundToMaxDecimalPlaces(n: number, ...references: number[]) {
|
||||
if (!isSafeNumber(n)) return 0;
|
||||
const maxPlaces = Math.max(...references.map((r) => getDecimalPlaces(r)));
|
||||
return Number(n.toFixed(maxPlaces));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user