Skip to content

Commit

Permalink
Fix some decimal logic with Odometer
Browse files Browse the repository at this point in the history
  • Loading branch information
nbass3 committed Feb 6, 2024
1 parent 8dbbce4 commit 615d63b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/component/Odometer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react';

interface OdometerProps {
value?: string;
value?: string | number;
spaceBefore?: string;
spaceAfter?: string;
zeroDecimals?: number;
Expand All @@ -18,10 +18,15 @@ export const Odometer = ({
decimalColor = 'var(--text-color-tertiary)',
}: OdometerProps) => {
const formattedValue = useMemo(() => {
let [whole, decimal] = value.split('.');
let [whole, decimal] = String(value).split('.');
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (decimal) {
decimal = parseFloat(`0.${decimal}`).toFixed(zeroDecimals).substring(2);
const factor = 10 ** zeroDecimals;
let result: number | string =
Math.floor(parseFloat(`0.${decimal}`) * factor) / factor;

result = result.toString().split('.')[1] || '';
decimal = result;
}
return { whole, decimal };
}, [value, zeroDecimals]);
Expand Down

0 comments on commit 615d63b

Please sign in to comment.