codeandtheory/YCharts

NumberFormatException: For input string: "48,09"

akardas16 opened this issue · 2 comments

I clone the project and when I run it on my device, app crashed in a few activities. the issue is related to string conversion to float.
As you see below value from Random.nextDouble(1.0, maxRange.toDouble()) is 48.088231731515215 but formating will cause to change point (.) to comma (,). this could be related to device language because we use comma for decimals in Turkish.
val barValue = "%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).toFloat()

getGroupBarChartData: 48.088231731515215 2023-08-21 00:34:23.560 10199-10199 AndroidRuntime co.yml.ycharts.app D Shutting down VM 2023-08-21 00:34:23.561 10199-10199 AndroidRuntime co.yml.ycharts.app E FATAL EXCEPTION: main Process: co.yml.ycharts.app, PID: 10199 java.lang.NumberFormatException: For input string: "48,09" at jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054) at jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122) at java.lang.Float.parseFloat(Float.java:455) at co.yml.charts.common.utils.DataUtils.getGroupBarChartData(DataUtils.kt:341) at co.yml.ycharts.app.presentation.CombinedLineAndBarChartActivityKt.BarWithLineChart(CombinedLineAndBarChartActivity.kt:104) at co.yml.ycharts.app.presentation.CombinedLineAndBarChartActivity$onCreate$1$1$2$1$1$1$1.invoke(CombinedLineAndBarChartActivity.kt:76) at co.yml.ycharts.app.presentation.CombinedLineAndBarChartActivity$onCreate$1$1$2$1$1$1$1.invoke(CombinedLineAndBarChartActivity.kt:67) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:135)

A simple hack is to edit four lines in DataUtils by adding a replace() call:

                    "%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).replace(',','.').toFloat(),

This simply brings the european (and other) comma-format back to the dot-format that 'toFloat' expects.

A simple hack is to edit four lines in DataUtils by adding a replace() call:

                    "%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).replace(',','.').toFloat(),

This simply brings the european (and other) comma-format back to the dot-format that 'toFloat' expects.

Why is that happen? Is that kotlin/java bug?