Skip to content

Commit

Permalink
Fix amount parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 17, 2023
1 parent eb4f3fc commit a9b3e7b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/src/main/java/com/ivy/wallet/ui/csv/domain/ParseFields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ private fun parsePositiveDouble(string: String): Double? {
val cleanedString = string
.replace("-", "")
.replace(" ", ".")
.filter { it.isDigit() || it == '.' }
val numberFormat = NumberFormat.getInstance(Locale.getDefault())
return if (numberFormat is DecimalFormat) {
numberFormat.applyPattern("#,###.##")
val parsedNumber = numberFormat.parse(cleanedString)
parsedNumber?.toDouble()
} else {
string.toDoubleOrNull()
if (numberFormat is DecimalFormat) {
try {
numberFormat.applyPattern("#,###.##")
val parsedNumber = numberFormat.parse(cleanedString)?.toDouble()
if (parsedNumber != null) return parsedNumber
} catch (e: Exception) {
// ignored
}
}

return cleanedString.toDoubleOrNull() ?: string.toDoubleOrNull()
}

// endregion
Expand Down

0 comments on commit a9b3e7b

Please sign in to comment.