Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit adf4720

Browse files
committed
Fix less than cent case to small sat amounts
1 parent c982a51 commit adf4720

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/action/payment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ class PaymentAction {
137137
return this.setAmount({ amount: '0' });
138138
}
139139
let amtSat = Math.floor(0.8 * balanceSatoshis);
140-
payment.amount = toAmount(amtSat, settings);
140+
payment.amount = toAmount(amtSat, settings, 2);
141141
await this.estimateFee();
142142
amtSat = balanceSatoshis - toSatoshis(payment.fee, settings);
143-
payment.amount = toAmount(amtSat, settings);
143+
payment.amount = toAmount(amtSat, settings, 2);
144144
payment.sendAll = true;
145145
}
146146

src/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const toSatoshis = (amount, settings) => {
8181
* @param {Object} settings Contains the current exchange rate
8282
* @return {string} The amount formatted as '0.0001'
8383
*/
84-
export const toAmount = (satoshis, settings) => {
84+
export const toAmount = (satoshis, settings, fiatPrecision = 8) => {
8585
if (
8686
!Number.isInteger(satoshis) ||
8787
!settings ||
@@ -95,7 +95,7 @@ export const toAmount = (satoshis, settings) => {
9595
const options = {
9696
useGrouping: false,
9797
minimumFractionDigits: settings.displayFiat ? 2 : 0,
98-
maximumFractionDigits: settings.displayFiat ? 2 : 8,
98+
maximumFractionDigits: settings.displayFiat ? fiatPrecision : 8,
9999
};
100100
return new Intl.NumberFormat('en-US', options).format(num);
101101
};

test/unit/helper.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,19 @@ describe('Helpers Unit Tests', () => {
363363
it('should work for number input (fiat)', () => {
364364
settings.displayFiat = true;
365365
const num = helpers.toAmount(100000, settings);
366+
expect(num, 'to equal', '6.89512515');
367+
});
368+
369+
it('should work for number input (fiat)', () => {
370+
settings.displayFiat = true;
371+
const num = helpers.toAmount(100000, settings, 2);
366372
expect(num, 'to equal', '6.90');
367373
});
368374

369375
it('should use period for decimals values (fiat)', () => {
370376
settings.displayFiat = true;
371377
const num = helpers.toAmount(100000000, settings);
372-
expect(num, 'to equal', '6895.13');
378+
expect(num, 'to equal', '6895.12514652');
373379
});
374380

375381
it('should work for 0 (fiat)', () => {
@@ -381,13 +387,13 @@ describe('Helpers Unit Tests', () => {
381387
it('should work for 1 (fiat)', () => {
382388
settings.displayFiat = true;
383389
const num = helpers.toAmount(1, settings);
384-
expect(num, 'to equal', '0.00');
390+
expect(num, 'to equal', '0.00006895');
385391
});
386392

387393
it('should work for 1000 (fiat)', () => {
388394
settings.displayFiat = true;
389395
const num = helpers.toAmount(1000, settings);
390-
expect(num, 'to equal', '0.07');
396+
expect(num, 'to equal', '0.06895125');
391397
});
392398
});
393399

0 commit comments

Comments
 (0)