FIX: round fiat currencies to 2 significant digits

This commit is contained in:
Ivan Vershigora 2020-06-03 15:25:38 +03:00 committed by Overtorment
parent 431e3fa37d
commit 6e54572402
6 changed files with 39 additions and 16 deletions

View file

@ -108,7 +108,7 @@ def enableProguardInReleaseBuilds = false
* give correct results when using with locales other than en-US. Note that * give correct results when using with locales other than en-US. Note that
* this variant is about 6MiB larger per architecture than default. * 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. * Whether to enable the Hermes VM.

View file

@ -86,20 +86,21 @@ function satoshiToLocalCurrency(satoshi) {
return '...'; return '...';
} }
let b = new BigNumber(satoshi); let b = new BigNumber(satoshi).dividedBy(100000000).multipliedBy(exchangeRates['BTC_' + preferredFiatCurrency.endPointKey]);
b = b
.dividedBy(100000000) if (b.isGreaterThanOrEqualTo(1) || b.isLessThanOrEqualTo(-1)) {
.multipliedBy(exchangeRates['BTC_' + preferredFiatCurrency.endPointKey]) b = b.toFixed(2);
.toString(10); } else {
b = parseFloat(b).toFixed(2); b = b.toPrecision(2);
}
let formatter; let formatter;
try { try {
formatter = new Intl.NumberFormat(preferredFiatCurrency.locale, { formatter = new Intl.NumberFormat(preferredFiatCurrency.locale, {
style: 'currency', style: 'currency',
currency: preferredFiatCurrency.endPointKey, currency: preferredFiatCurrency.endPointKey,
minimumFractionDigits: 2, minimumFractionDigits: 2,
maximumFractionDigits: 8,
}); });
} catch (error) { } catch (error) {
console.warn(error); console.warn(error);
@ -108,6 +109,7 @@ function satoshiToLocalCurrency(satoshi) {
style: 'currency', style: 'currency',
currency: preferredFiatCurrency.endPointKey, currency: preferredFiatCurrency.endPointKey,
minimumFractionDigits: 2, minimumFractionDigits: 2,
maximumFractionDigits: 8,
}); });
} }
@ -135,3 +137,4 @@ module.exports.satoshiToBTC = satoshiToBTC;
module.exports.BTCToLocalCurrency = BTCToLocalCurrency; module.exports.BTCToLocalCurrency = BTCToLocalCurrency;
module.exports.setPrefferedCurrency = setPrefferedCurrency; module.exports.setPrefferedCurrency = setPrefferedCurrency;
module.exports.getPreferredCurrency = getPreferredCurrency; module.exports.getPreferredCurrency = getPreferredCurrency;
module.exports.exchangeRates = exchangeRates; // export it to mock data in tests

View file

@ -1,5 +1,3 @@
import 'intl';
import 'intl/locale-data/jsonp/en';
import React from 'react'; import React from 'react';
import './shim.js'; import './shim.js';
import { AppRegistry } from 'react-native'; import { AppRegistry } from 'react-native';

5
package-lock.json generated
View file

@ -8011,11 +8011,6 @@
"side-channel": "^1.0.2" "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": { "invariant": {
"version": "2.2.4", "version": "2.2.4",
"resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz", "resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz",

View file

@ -91,7 +91,6 @@
"eslint-plugin-standard": "4.0.1", "eslint-plugin-standard": "4.0.1",
"events": "1.1.1", "events": "1.1.1",
"frisbee": "3.1.2", "frisbee": "3.1.2",
"intl": "1.2.5",
"lottie-react-native": "3.1.1", "lottie-react-native": "3.1.1",
"metro-react-native-babel-preset": "0.59.0", "metro-react-native-babel-preset": "0.59.0",
"node-libs-react-native": "1.2.0", "node-libs-react-native": "1.2.0",

View file

@ -30,4 +30,32 @@ describe('currency', () => {
cur = JSON.parse(await AsyncStorage.getItem(AppStorage.EXCHANGE_RATES)); cur = JSON.parse(await AsyncStorage.getItem(AppStorage.EXCHANGE_RATES));
assert.ok(cur.BTC_EUR > 0); 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');
});
}); });