mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
35 lines
695 B
TypeScript
35 lines
695 B
TypeScript
import BadDataException from "./Exception/BadDataException";
|
|
|
|
export default class Currency {
|
|
public static convertToDecimalPlaces(
|
|
value: number,
|
|
decimalPlaces: number = 2,
|
|
): number {
|
|
if (decimalPlaces < 0) {
|
|
throw new BadDataException(
|
|
"decimalPlaces must be greater than or equal to 0.",
|
|
);
|
|
}
|
|
|
|
if (typeof value === "string") {
|
|
value = parseFloat(value);
|
|
}
|
|
|
|
if (decimalPlaces === 0) {
|
|
return Math.ceil(value);
|
|
}
|
|
|
|
value = value * Math.pow(10, decimalPlaces);
|
|
|
|
// convert to int.
|
|
|
|
value = Math.round(value);
|
|
|
|
// convert back to float.
|
|
|
|
value = value / Math.pow(10, decimalPlaces);
|
|
|
|
return value;
|
|
}
|
|
}
|