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

Revert "Round sat amounts to 3 decimals" #1587

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions components/Sats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Row align="flex-end">
<Body>{fiatStore.numberWithCommas(sats, 3)}</Body>
<Body>{numberWithCommas(sats)}</Body>
<Spacer width={2} />
<View style={{ paddingBottom: 1.5 }}>
<Body secondary small>
Expand Down
28 changes: 6 additions & 22 deletions stores/FiatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
22 changes: 12 additions & 10 deletions stores/UnitsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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}`;
Expand Down