mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-04 04:13:49 +01:00
FIX: round fiat currencies to 2 significant digits
This commit is contained in:
parent
431e3fa37d
commit
6e54572402
6 changed files with 39 additions and 16 deletions
|
@ -108,7 +108,7 @@ def enableProguardInReleaseBuilds = false
|
|||
* give correct results when using with locales other than en-US. Note that
|
||||
* this variant is about 6MiB larger per architecture than default.
|
||||
*/
|
||||
def jscFlavor = 'org.webkit:android-jsc:+'
|
||||
def jscFlavor = 'org.webkit:android-jsc-intl:+'
|
||||
|
||||
/**
|
||||
* Whether to enable the Hermes VM.
|
||||
|
|
17
currency.js
17
currency.js
|
@ -86,20 +86,21 @@ function satoshiToLocalCurrency(satoshi) {
|
|||
return '...';
|
||||
}
|
||||
|
||||
let b = new BigNumber(satoshi);
|
||||
b = b
|
||||
.dividedBy(100000000)
|
||||
.multipliedBy(exchangeRates['BTC_' + preferredFiatCurrency.endPointKey])
|
||||
.toString(10);
|
||||
b = parseFloat(b).toFixed(2);
|
||||
let b = new BigNumber(satoshi).dividedBy(100000000).multipliedBy(exchangeRates['BTC_' + preferredFiatCurrency.endPointKey]);
|
||||
|
||||
if (b.isGreaterThanOrEqualTo(1) || b.isLessThanOrEqualTo(-1)) {
|
||||
b = b.toFixed(2);
|
||||
} else {
|
||||
b = b.toPrecision(2);
|
||||
}
|
||||
|
||||
let formatter;
|
||||
|
||||
try {
|
||||
formatter = new Intl.NumberFormat(preferredFiatCurrency.locale, {
|
||||
style: 'currency',
|
||||
currency: preferredFiatCurrency.endPointKey,
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 8,
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn(error);
|
||||
|
@ -108,6 +109,7 @@ function satoshiToLocalCurrency(satoshi) {
|
|||
style: 'currency',
|
||||
currency: preferredFiatCurrency.endPointKey,
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 8,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -135,3 +137,4 @@ module.exports.satoshiToBTC = satoshiToBTC;
|
|||
module.exports.BTCToLocalCurrency = BTCToLocalCurrency;
|
||||
module.exports.setPrefferedCurrency = setPrefferedCurrency;
|
||||
module.exports.getPreferredCurrency = getPreferredCurrency;
|
||||
module.exports.exchangeRates = exchangeRates; // export it to mock data in tests
|
||||
|
|
2
index.js
2
index.js
|
@ -1,5 +1,3 @@
|
|||
import 'intl';
|
||||
import 'intl/locale-data/jsonp/en';
|
||||
import React from 'react';
|
||||
import './shim.js';
|
||||
import { AppRegistry } from 'react-native';
|
||||
|
|
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -8011,11 +8011,6 @@
|
|||
"side-channel": "^1.0.2"
|
||||
}
|
||||
},
|
||||
"intl": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.npmjs.org/intl/-/intl-1.2.5.tgz",
|
||||
"integrity": "sha1-giRKIZDE5Bn4Nx9ao02qNCDiq94="
|
||||
},
|
||||
"invariant": {
|
||||
"version": "2.2.4",
|
||||
"resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz",
|
||||
|
|
|
@ -91,7 +91,6 @@
|
|||
"eslint-plugin-standard": "4.0.1",
|
||||
"events": "1.1.1",
|
||||
"frisbee": "3.1.2",
|
||||
"intl": "1.2.5",
|
||||
"lottie-react-native": "3.1.1",
|
||||
"metro-react-native-babel-preset": "0.59.0",
|
||||
"node-libs-react-native": "1.2.0",
|
||||
|
|
|
@ -30,4 +30,32 @@ describe('currency', () => {
|
|||
cur = JSON.parse(await AsyncStorage.getItem(AppStorage.EXCHANGE_RATES));
|
||||
assert.ok(cur.BTC_EUR > 0);
|
||||
});
|
||||
|
||||
it('formats everything correctly', async () => {
|
||||
const currency = require('../../currency');
|
||||
await currency.setPrefferedCurrency(FiatUnit.USD);
|
||||
await currency.startUpdater();
|
||||
currency.exchangeRates.BTC_USD = 10000;
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(1), '$0.0001');
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(-1), '-$0.0001');
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(123), '$0.012');
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(146), '$0.015');
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(123456789), '$12,345.68');
|
||||
|
||||
assert.strictEqual(currency.BTCToLocalCurrency(1), '$10,000.00');
|
||||
assert.strictEqual(currency.BTCToLocalCurrency(-1), '-$10,000.00');
|
||||
assert.strictEqual(currency.BTCToLocalCurrency(1.00000001), '$10,000.00');
|
||||
assert.strictEqual(currency.BTCToLocalCurrency(1.0000123), '$10,000.12');
|
||||
assert.strictEqual(currency.BTCToLocalCurrency(1.0000146), '$10,000.15');
|
||||
|
||||
assert.strictEqual(currency.satoshiToBTC(1), '0.00000001');
|
||||
assert.strictEqual(currency.satoshiToBTC(-1), '-0.00000001');
|
||||
assert.strictEqual(currency.satoshiToBTC(100000000), '1');
|
||||
assert.strictEqual(currency.satoshiToBTC(123456789123456789), '1234567891.2345678');
|
||||
|
||||
await currency.setPrefferedCurrency(FiatUnit.JPY);
|
||||
await currency.startUpdater();
|
||||
currency.exchangeRates.BTC_JPY = 1043740.8614;
|
||||
assert.strictEqual(currency.satoshiToLocalCurrency(1), '¥0.01');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue