Skip to content

Entering weight with Dutch (🇳🇱) locale fails #62

@Donkere-vader

Description

@Donkere-vader

Description

On the profile page after typing in my weight, i get the error message "Bad Value" underneath the number input field.

Reason hypothesis

I see that the input value is parsed as follows;

    // app/src/main/java/com/darkrockstudios/apps/fasttrack/screens/profile/ProfileViewModel.kt
    // line 294 - 304
    private fun parseDouble(text: String?): Double {
        var number = 0.0
        text?.let {
            try {
                number = text.toDouble()
            } catch (e: NumberFormatException) {
                Napier.w("Failed to parse double")
            }
        }
        return number
    }

The problem here is that String.toDouble() expects a double to be formatted as [0-9]*.[0-9]* (sorry for the ReGex if you're not well versed in it) basically, it expects the decimal point to be an actual ".".
This is true for english, but in other locales, suchs as in Dutch (🇳🇱) the "decimal point" is actually a comma (,).

It's quite a few countries actually;
(wikipedia) Decimal separator

So if I were 50.5 Kg, I enter 50,5. And as the input field seems to respect my locale, it uses a "," to seperate the decimals from the rest of the number.

Which then results in the error. And I am unable to use a "." because the input field will still force in the ",0" at the end.

TL;DR

The input field respects my locale so forces a , decimal separator, but the validation code expects a . as decimal separator.

Minimal example

With ,

fun main() {
    println("12,0".toDouble());
}
vscode ➜ /workspaces/test $ ./gradlew run

> Task :run FAILED
Exception in thread "main" java.lang.NumberFormatException: For input string: "12,0"
        at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
        at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)

With .

fun main() {
    println("12.0".toDouble());
}
vscode ➜ /workspaces/test $ ./gradlew run

> Task :run
12.0

Closing word

Thanks for making this app, I enjoy using it.

And sorry for not making a PR out of this, I tried for a bit to set up a devcontainer and all. but it didn't go very smoothly, and I do not feel like setting up an entire android dev environment for this simple bug. As I do not have any kotlin/ android stuff set up on my system.

LMK if you need more details info on this :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions