Skip to content

Commit

Permalink
Merge pull request #1396 from kaloudis/show-all-decimal-places
Browse files Browse the repository at this point in the history
Settings: Display: Show all decimal places
  • Loading branch information
kaloudis authored Apr 1, 2023
2 parents 3e2d983 + f22c48b commit 20ced06
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 12 deletions.
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
"views.Settings.Display.defaultView": "Default view",
"views.Settings.Display.displayNickname": "Display node nickname on main views",
"views.Settings.Display.bigKeypadButtons": "Big keypad buttons",
"views.Settings.Display.showAllDecimalPlaces": "Show all decimal places",
"views.Settings.privacy": "Privacy",
"views.Settings.payments": "Payments",
"views.Settings.Privacy.title": "Privacy settings",
Expand Down
4 changes: 3 additions & 1 deletion stores/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface DisplaySettings {
defaultView?: string;
displayNickname?: boolean;
bigKeypadButtons?: boolean;
showAllDecimalPlaces?: boolean;
}

interface PosSettings {
Expand Down Expand Up @@ -260,7 +261,8 @@ export default class SettingsStore {
theme: DEFAULT_THEME,
defaultView: 'Keypad',
displayNickname: false,
bigKeypadButtons: false
bigKeypadButtons: false,
showAllDecimalPlaces: false
},
pos: {
squareEnabled: false,
Expand Down
9 changes: 7 additions & 2 deletions stores/UnitsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export default class UnitsStore {
fixedUnits?: string
): ValueDisplayProps => {
const { settings } = this.settingsStore;
const { fiat } = settings;
const { fiat, display } = settings;
const showAllDecimalPlaces: boolean =
(display && display.showAllDecimalPlaces) || false;
const units = fixedUnits || this.units;

const sats = Number(value);
Expand All @@ -69,7 +71,10 @@ export default class UnitsStore {

if (units === 'BTC') {
return {
amount: FeeUtils.toFixed(absValueSats / SATS_PER_BTC),
amount: FeeUtils.toFixed(
absValueSats / SATS_PER_BTC,
showAllDecimalPlaces
),
unit: 'BTC',
negative,
space: false
Expand Down
28 changes: 28 additions & 0 deletions utils/FeeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,33 @@ describe('FeeUtils', () => {
'-0.005'
);
});

it('Properly handles decimals in Bitcoin unit format - with showAllDecimalPlaces enabled', () => {
expect(FeeUtils.toFixed(100 / satoshisPerBTC, true)).toEqual(
'0.00000100'
);
expect(FeeUtils.toFixed(1000 / satoshisPerBTC, true)).toEqual(
'0.00001000'
);
expect(FeeUtils.toFixed(10000 / satoshisPerBTC, true)).toEqual(
'0.00010000'
);
// was returning "0.00000009999999999999999" in original version
expect(
FeeUtils.toFixed(Number('10') / satoshisPerBTC, true).toString()
).toBe('0.00000010');
expect(FeeUtils.toFixed(1 / satoshisPerBTC, true)).toEqual(
'0.00000001'
);
expect(FeeUtils.toFixed(283190 / satoshisPerBTC, true)).toEqual(
'0.00283190'
);
expect(FeeUtils.toFixed(500000 / satoshisPerBTC, true)).toEqual(
'0.00500000'
);
expect(FeeUtils.toFixed(-500000 / satoshisPerBTC, true)).toEqual(
'-0.00500000'
);
});
});
});
3 changes: 2 additions & 1 deletion utils/FeeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class FeeUtils {

return split[0];
};
toFixed = (x: any) => {
toFixed = (x: any, showAllDecimalPlaces?: boolean) => {
if (showAllDecimalPlaces) return x.toFixed(8);
return x.toFixed(8).replace(/\.?0+$/, '');
};
}
Expand Down
77 changes: 69 additions & 8 deletions views/Settings/Display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface DisplayState {
defaultView: string;
displayNickname: boolean;
bigKeypadButtons: boolean;
showAllDecimalPlaces: boolean;
}

@inject('SettingsStore')
Expand All @@ -35,7 +36,8 @@ export default class Display extends React.Component<
theme: 'Dark',
defaultView: 'Keypad',
displayNickname: false,
bigKeypadButtons: false
bigKeypadButtons: false,
showAllDecimalPlaces: false
};

async UNSAFE_componentWillMount() {
Expand All @@ -50,7 +52,11 @@ export default class Display extends React.Component<
displayNickname:
(settings.display && settings.display.displayNickname) || false,
bigKeypadButtons:
(settings.display && settings.display.bigKeypadButtons) || false
(settings.display && settings.display.bigKeypadButtons) ||
false,
showAllDecimalPlaces:
(settings.display && settings.display.showAllDecimalPlaces) ||
false
});
}

Expand All @@ -65,8 +71,13 @@ export default class Display extends React.Component<

render() {
const { navigation, SettingsStore } = this.props;
const { defaultView, displayNickname, bigKeypadButtons, theme } =
this.state;
const {
defaultView,
displayNickname,
bigKeypadButtons,
theme,
showAllDecimalPlaces
} = this.state;
const { updateSettings }: any = SettingsStore;

const BackButton = () => (
Expand Down Expand Up @@ -111,7 +122,8 @@ export default class Display extends React.Component<
theme: value,
displayNickname,
bigKeypadButtons,
defaultView
defaultView,
showAllDecimalPlaces
}
});
}}
Expand All @@ -132,7 +144,8 @@ export default class Display extends React.Component<
defaultView: value,
displayNickname,
bigKeypadButtons,
theme
theme,
showAllDecimalPlaces
}
});
}}
Expand Down Expand Up @@ -174,7 +187,8 @@ export default class Display extends React.Component<
defaultView,
theme,
bigKeypadButtons,
displayNickname: !displayNickname
displayNickname: !displayNickname,
showAllDecimalPlaces
}
});
}}
Expand Down Expand Up @@ -217,7 +231,54 @@ export default class Display extends React.Component<
defaultView,
theme,
displayNickname,
bigKeypadButtons: !bigKeypadButtons
bigKeypadButtons: !bigKeypadButtons,
showAllDecimalPlaces
}
});
}}
/>
</View>
</ListItem>

<ListItem
containerStyle={{
borderBottomWidth: 0,
backgroundColor: 'transparent'
}}
>
<ListItem.Title
style={{
color: themeColor('secondaryText'),
fontFamily: 'Lato-Regular',
left: -10
}}
>
{localeString(
'views.Settings.Display.showAllDecimalPlaces'
)}
</ListItem.Title>
<View
style={{
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end'
}}
>
<Switch
value={showAllDecimalPlaces}
onValueChange={async () => {
this.setState({
showAllDecimalPlaces:
!showAllDecimalPlaces
});
await updateSettings({
display: {
defaultView,
theme,
displayNickname,
bigKeypadButtons,
showAllDecimalPlaces:
!showAllDecimalPlaces
}
});
}}
Expand Down

0 comments on commit 20ced06

Please sign in to comment.