diff --git a/components/Sats.tsx b/components/Sats.tsx index ea3457ac0..97b5f7b89 100644 --- a/components/Sats.tsx +++ b/components/Sats.tsx @@ -3,15 +3,15 @@ import { View } from 'react-native'; import { Body } from '../components/text/Body'; import { Row } from '../components/layout/Row'; import { Spacer } from '../components/layout/Spacer'; -import stores from '../stores/Stores'; // TODO: will replace this with a more generic "Value" component export function Sats({ sats }: { sats: number }) { - const fiatStore = stores.fiatStore; + const numberWithCommas = (x: string | number = 0) => + x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); return ( - {fiatStore.numberWithCommas(sats, 3)} + {numberWithCommas(sats)} diff --git a/stores/FiatStore.ts b/stores/FiatStore.ts index ff8810315..778253d97 100644 --- a/stores/FiatStore.ts +++ b/stores/FiatStore.ts @@ -33,29 +33,13 @@ export default class FiatStore { this.settingsStore = settingsStore; } - numberWithCommas = ( - x: string | number, - maximumFractionDigits: number = 11, - minimumFractionDigits?: number - ) => - Number(x) - .toLocaleString(undefined, { - maximumFractionDigits, - minimumFractionDigits, - useGrouping: true - }) - .replace(/\B(?=(\d{3})+(?!\d))/g, ','); + numberWithCommas = (x: string | number) => + x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); - numberWithDecimals = ( - x: string | number, - maximumFractionDigits?: number, - minimumFractionDigits?: number - ) => - this.numberWithCommas( - x, - maximumFractionDigits, - minimumFractionDigits - ).replace(/[,.]/g, (y: string) => (y === ',' ? '.' : ',')); + numberWithDecimals = (x: string | number) => + this.numberWithCommas(x).replace(/[,.]/g, (y: string) => + y === ',' ? '.' : ',' + ); // Resource below may be helpful for formatting // https://fastspring.com/blog/how-to-format-30-currencies-from-countries-all-over-the-world/ diff --git a/stores/UnitsStore.ts b/stores/UnitsStore.ts index d35fa9885..45062f38a 100644 --- a/stores/UnitsStore.ts +++ b/stores/UnitsStore.ts @@ -81,7 +81,7 @@ export default class UnitsStore { }; } else if (units === 'sats') { return { - amount: this.fiatStore.numberWithCommas(absValueSats, 3), + amount: this.fiatStore.numberWithCommas(absValueSats), unit: 'sats', negative, plural: !(Number(value) === 1 || Number(value) === -1) @@ -113,13 +113,14 @@ export default class UnitsStore { const { symbol, space, rtl, separatorSwap } = this.fiatStore.getSymbol(); - const amount = - FeeUtils.toFixed(absValueSats / SATS_PER_BTC) * rate; + const amount = ( + FeeUtils.toFixed(absValueSats / SATS_PER_BTC) * rate + ).toFixed(2); return { amount: separatorSwap - ? this.fiatStore.numberWithDecimals(amount, 2, 2) - : this.fiatStore.numberWithCommas(amount, 2, 2), + ? this.fiatStore.numberWithDecimals(amount) + : this.fiatStore.numberWithCommas(amount), unit: 'fiat', symbol, negative, @@ -154,7 +155,7 @@ export default class UnitsStore { Number(wholeSats || 0) / SATS_PER_BTC )}`; } else if (units === 'sats') { - const sats = `${this.fiatStore.numberWithCommas(value, 3) || 0} ${ + const sats = `${this.fiatStore.numberWithCommas(value) || 0} ${ Number(value) === 1 || Number(value) === -1 ? 'sat' : 'sats' }`; return sats; @@ -168,13 +169,14 @@ export default class UnitsStore { const { symbol, space, rtl, separatorSwap } = this.fiatStore.symbolLookup(code); - const amount = + const amount = ( FeeUtils.toFixed(Number(wholeSats || 0) / SATS_PER_BTC) * - rate; + rate + ).toFixed(2); const formattedAmount = separatorSwap - ? this.fiatStore.numberWithDecimals(amount, 2, 2) - : this.fiatStore.numberWithCommas(amount, 2, 2); + ? this.fiatStore.numberWithDecimals(amount) + : this.fiatStore.numberWithCommas(amount); if (rtl) { return `${formattedAmount}${space ? ' ' : ''}${symbol}`;