@jk-core/components
Version:
components for jk
31 lines (24 loc) • 1.68 kB
text/typescript
import { roundNum } from '@jk-core/utils';
const KiloToMega = (amount: number | null | undefined, unit = 'Wh', scale = 2, isKilo = true) => {
const validAmount = amount ?? 0;
if (!isKilo) {
if (roundNum(validAmount / Math.pow(10, 9)) >= 1) {
return { generation: roundNum(validAmount / Math.pow(10, 9), scale) || 0, unit: `G${unit}`, total: `${roundNum(validAmount / Math.pow(10, 9), scale)} G${unit}` };
}
if (roundNum(validAmount / Math.pow(10, 6)) >= 1) {
return { generation: roundNum(validAmount / Math.pow(10, 6), scale) || 0, unit: `M${unit}`, total: `${roundNum(validAmount / Math.pow(10, 6), scale)} M${unit}` };
}
if (roundNum(validAmount / Math.pow(10, 3)) >= 1) {
return { generation: roundNum(validAmount / Math.pow(10, 3), scale), unit: `k${unit}`, total: `${roundNum(validAmount / Math.pow(10, 3), scale).toLocaleString()} k${unit}` };
}
return { generation: roundNum(validAmount, scale), unit: `${unit}`, total: `${roundNum(validAmount, scale).toLocaleString()} ${unit}` };
}
if (roundNum(validAmount / Math.pow(10, 6)) >= 1) {
return { generation: roundNum(validAmount / Math.pow(10, 6), scale) || 0, unit: `G${unit}`, total: `${roundNum(validAmount / Math.pow(10, 6), scale)} G${unit}` };
}
if (roundNum(validAmount / Math.pow(10, 3)) >= 1) {
return { generation: roundNum(validAmount / Math.pow(10, 3), scale), unit: `M${unit}`, total: `${roundNum(validAmount / Math.pow(10, 3), scale).toLocaleString()} M${unit}` };
}
return { generation: roundNum(validAmount, scale), unit: `k${unit}`, total: `${roundNum(validAmount, scale).toLocaleString()} k${unit}` };
};
export default KiloToMega;