Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix smh converter #267

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/helper/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,31 @@ const CoinUnits = {
Smidge: 'Smidge',
};

// Truncates a number to a specified number of decimal places without rounding
const truncateToDecimalPlaces = (num: number, decimalPlaces: number) => {
const numPower = 10 ** decimalPlaces;
return Math.floor(num * numPower) / numPower;
};

const packValueAndUnit = (value: number, unit: string) => ({
value: Number.isInteger(value) ? value.toString() : value.toFixed(3),
value: value.toString(),
unit,
});

export const toSMH = (smidge: number) => smidge / 10 ** 9;
export const toSmidge = (smh: number) => Math.ceil(smh * 10 ** 9);
export const toSmidge = (smh: number) => Math.round(smh * 10 ** 9);

// Parses number into { value, unit } format.
// Used to format smidge strings
export const parseSmidge = (amount: number) => {
// If amount is "falsy" (0 | undefined | null)
if (!amount) return packValueAndUnit(0, CoinUnits.SMH);
// Show `23.053 SMH` for big amount
if (amount >= 10 ** 6) return packValueAndUnit(toSMH(amount), CoinUnits.SMH);
// Or `6739412 Smidge` (without dot) for small amount
if (amount >= 10 ** 9) {
// Truncate to 3 decimal places without rounding
const smhValue = truncateToDecimalPlaces(toSMH(amount), 3);
return packValueAndUnit(smhValue, CoinUnits.SMH);
// Or `6739412 Smidge` (without dot) for small amount
}
if (!Number.isNaN(amount)) return packValueAndUnit(amount, CoinUnits.Smidge);
// Show `0 SMH` for zero amount and NaN
return packValueAndUnit(0, CoinUnits.SMH);
Expand Down
Loading