From 46f12ae0ebca2639f40829cdc6c4c6182e202f29 Mon Sep 17 00:00:00 2001 From: ssabeerahamed Date: Wed, 12 May 2021 20:14:45 +0530 Subject: [PATCH] DTSERWFOUR-558 rounding fx value --- .../android/ui/common/util/CurrencyParser.java | 15 ++++++--------- .../ui/common/util/CurrencyParserTest.java | 6 +++--- .../ui/transfer/TransferUserFundsTest.java | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/commonui/src/main/java/com/hyperwallet/android/ui/common/util/CurrencyParser.java b/commonui/src/main/java/com/hyperwallet/android/ui/common/util/CurrencyParser.java index 46998a5fc..999b42666 100644 --- a/commonui/src/main/java/com/hyperwallet/android/ui/common/util/CurrencyParser.java +++ b/commonui/src/main/java/com/hyperwallet/android/ui/common/util/CurrencyParser.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.io.InputStream; +import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; @@ -137,15 +138,11 @@ public CurrencyDetails getCurrency(String currencyCode) { */ public static String getValueWithTruncateDecimals(String value, int noOfDecimals) { if (value != null) { - String returnValue = value; - int decimalLength = 0; - if (value.contains(".")) { - decimalLength = value.substring(value.indexOf(".")).length(); - } - if (decimalLength > noOfDecimals) { - returnValue = value.substring(0, value.indexOf(".") + noOfDecimals + 1); - } - return returnValue; + NumberFormat nf = NumberFormat.getNumberInstance(); + nf.setMaximumFractionDigits(noOfDecimals); + nf.setRoundingMode(RoundingMode.HALF_UP); + double amount = Double.parseDouble(value); + return nf.format(amount); } else { return ""; } diff --git a/commonui/src/test/java/com/hyperwallet/android/ui/common/util/CurrencyParserTest.java b/commonui/src/test/java/com/hyperwallet/android/ui/common/util/CurrencyParserTest.java index 6c76b40da..86c17a85c 100644 --- a/commonui/src/test/java/com/hyperwallet/android/ui/common/util/CurrencyParserTest.java +++ b/commonui/src/test/java/com/hyperwallet/android/ui/common/util/CurrencyParserTest.java @@ -122,12 +122,12 @@ public void testGetNumberOfFractionDigits_NoDigitDecimal(){ @Test public void testFXRateWithFourDecimal() { - assertThat("1.2345",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",4))); + assertThat("1.2346",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",4))); assertThat("1.234",is(CurrencyParser.getValueWithTruncateDecimals("1.234",4))); - assertThat("1.0000",is(CurrencyParser.getValueWithTruncateDecimals("1.000056",4))); + assertThat("1.0001",is(CurrencyParser.getValueWithTruncateDecimals("1.000056",4))); assertThat("1",is(CurrencyParser.getValueWithTruncateDecimals("1",4))); assertThat("",is(CurrencyParser.getValueWithTruncateDecimals(null,4))); - assertThat("1.234",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",3))); + assertThat("1.235",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",3))); assertThat("1.23",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",2))); assertThat("1.2",is(CurrencyParser.getValueWithTruncateDecimals("1.234567",1))); } diff --git a/transferui/src/androidTest/java/com/hyperwallet/android/ui/transfer/TransferUserFundsTest.java b/transferui/src/androidTest/java/com/hyperwallet/android/ui/transfer/TransferUserFundsTest.java index 236a838fa..e4cafd035 100644 --- a/transferui/src/androidTest/java/com/hyperwallet/android/ui/transfer/TransferUserFundsTest.java +++ b/transferui/src/androidTest/java/com/hyperwallet/android/ui/transfer/TransferUserFundsTest.java @@ -486,7 +486,7 @@ public void onReceive(Context context, Intent intent) { allOf(withId(R.id.exchange_rate_label), withText(R.string.mobileFXRateLabel)))))); onView(withId(R.id.list_foreign_exchange)).check( matches(atPosition(0, - hasDescendant(allOf(withId(R.id.exchange_rate_value), withText("$1 USD = $1.2912 CAD")))))); + hasDescendant(allOf(withId(R.id.exchange_rate_value), withText("$1 USD = $1.2913 CAD")))))); onView(withId(R.id.amount_label)).check(matches(withText(R.string.mobileConfirmDetailsAmount))); onView(withId(R.id.amount_value)).check(matches(withText("$152.20 CAD"))); onView(withId(R.id.fee_label)).check(matches(withText(R.string.mobileConfirmDetailsFee)));