diff --git a/android/app/build.gradle b/android/app/build.gradle index ea21e2597..e8cf87c4e 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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. diff --git a/currency.js b/currency.js index 859f3010f..8a4abc515 100644 --- a/currency.js +++ b/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 diff --git a/index.js b/index.js index 56852855e..4575d5f73 100644 --- a/index.js +++ b/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'; diff --git a/package-lock.json b/package-lock.json index 9f2c60d81..2643b5aff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 4cddc019a..913e0418b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/integration/Currency.test.js b/tests/integration/Currency.test.js index 861af96c0..5f42ba9d4 100644 --- a/tests/integration/Currency.test.js +++ b/tests/integration/Currency.test.js @@ -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'); + }); });