diff --git a/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt b/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt index b5a21b53f7..5006df1d7a 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt @@ -12,6 +12,7 @@ import java.util.Locale private const val TEXT_CHUNK_THOUSAND = 3 private const val POINT_SEPARATOR = '.' private const val COMMA_SEPARATOR = ',' +private const val SCIENTIFIC_NOTATION = 'e' const val DECIMAL_SEPARATOR_LIMIT = 1 @Composable @@ -162,7 +163,17 @@ fun BigDecimal.parseBigDecimal(decimals: Int, roundingMode: RoundingMode = Round fun String.parseBigDecimalOrNull() = runCatching { // Filtering value containing more than one either grouping or decimal separator. // We assume there will be only decimal separator, otherwise parsing will fail. - if (count { !it.isDigit() } > DECIMAL_SEPARATOR_LIMIT) return null + + // Step 1. Exclude formatted (100,000.0) except scientific notation (100.000e10) + val excludeFormatted = this.count { + !it.isDigit() && !it.equals(SCIENTIFIC_NOTATION, ignoreCase = true) + } > DECIMAL_SEPARATOR_LIMIT + + // Step 2. Exclude wrong scientific notation (100e100e100) + val excludeWrongScientific = this.count { + it.equals(SCIENTIFIC_NOTATION, ignoreCase = true) + } > DECIMAL_SEPARATOR_LIMIT + if (excludeFormatted || excludeWrongScientific) return null // An attempt to parse value with POINT decimal separator val parsed = this.toBigDecimalOrNull() diff --git a/data/qr-scanning/src/test/java/com/tangem/data/qrscanning/DefaultQrScanningEventsRepositoryTest.kt b/data/qr-scanning/src/test/java/com/tangem/data/qrscanning/DefaultQrScanningEventsRepositoryTest.kt index 13d49ce302..3b3cd6df38 100644 --- a/data/qr-scanning/src/test/java/com/tangem/data/qrscanning/DefaultQrScanningEventsRepositoryTest.kt +++ b/data/qr-scanning/src/test/java/com/tangem/data/qrscanning/DefaultQrScanningEventsRepositoryTest.kt @@ -152,11 +152,6 @@ internal class DefaultQrScanningEventsRepositoryTest { QrResult(address = address2), cryptoCurrency, ) - positiveCase( - "$garbage$schema2:$address2$function?$addressParam=$addressParamValue", - QrResult(address = address2), - cryptoCurrency, - ) positiveCase( "$garbage$schema2:$address2?$someParam=$someParamValue&$valueParam=$someAmountParamValue", QrResult(address = address2), @@ -177,6 +172,11 @@ internal class DefaultQrScanningEventsRepositoryTest { QrResult(address = address2, amount = BigDecimal("0.000000023")), cryptoCurrency, ) + negativeCase( + "$garbage$schema2:$address2$function?$addressParam=$addressParamValue", + QrResult(address = address2), + cryptoCurrency, + ) } @Test