Skip to content

Commit

Permalink
Fix price input verification taking locale specific number formatting…
Browse files Browse the repository at this point in the history
… into consideration
  • Loading branch information
DennisBauer committed Feb 2, 2024
1 parent 6874b48 commit f85e1a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/de/dbauer/expensetracker/Extensions.kt
Expand Up @@ -17,9 +17,10 @@ fun Float.toLocalString(): String {
}
}

fun String.toFloatIgnoreSeparator(): Float {
val converted = replace(",", ".")
return converted.toFloat()
fun String.toFloatLocaleAware(): Float? {
return NumberFormat.getInstance().let {
it.parse(this)?.toFloat()
}
}

fun ZipInputStream.forEachEntry(block: (entry: ZipEntry) -> Unit) {
Expand Down
Expand Up @@ -39,7 +39,7 @@ import androidx.compose.ui.unit.dp
import de.dbauer.expensetracker.R
import de.dbauer.expensetracker.data.Recurrence
import de.dbauer.expensetracker.data.RecurringExpenseData
import de.dbauer.expensetracker.toFloatIgnoreSeparator
import de.dbauer.expensetracker.toFloatLocaleAware
import de.dbauer.expensetracker.toLocalString
import de.dbauer.expensetracker.ui.customizations.ExpenseColor
import de.dbauer.expensetracker.ui.theme.ExpenseTrackerTheme
Expand Down Expand Up @@ -252,8 +252,8 @@ private fun onConfirmClicked(
id = currentData?.id ?: 0,
name = name,
description = description,
price = price.toFloatIgnoreSeparator(),
monthlyPrice = price.toFloatIgnoreSeparator(),
price = price.toFloatLocaleAware() ?: 0f,
monthlyPrice = price.toFloatLocaleAware() ?: 0f,
everyXRecurrence = everyXRecurrence.toIntOrNull() ?: 1,
recurrence = selectedRecurrence,
firstPayment = firstPayment,
Expand Down Expand Up @@ -292,8 +292,7 @@ private fun isNameValid(name: String): Boolean {
}

private fun isPriceValid(price: String): Boolean {
val priceConverted = price.replace(",", ".")
return priceConverted.toFloatOrNull() != null
return price.toFloatLocaleAware() != null
}

private fun isEveryXRecurrenceValid(everyXRecurrence: String): Boolean {
Expand Down

0 comments on commit f85e1a2

Please sign in to comment.