0

each solution I've found for this uses java rather than Kotlin, please can anyone help? I've written an app and I'm just trying to translate to some European countries, however the math part is falling over as Germans, French etc use commas rather than a full stop. I can't figure out how to fix this with the solutions on here.

part of .kt file:

import java.math.RoundingMode
import java.text.DecimalFormat
...
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)

any help appreciated thanks

EDIT: I have looked here: How to change the decimal separator of DecimalFormat from comma to dot/point? but as I said originally, all the solutions are for Java, not Kotlin

Maff
  • 441
  • 1
  • 6
  • 27
  • The fact your using Kotlin makes no real difference here: you are using the Java API, so the answers in the duplicate apply, because they are more about API usage than about language (Java vs Kotlin). – Mark Rotteveel Aug 21 '19 at 17:59

1 Answers1

1

I can't try it at the moment but something like

val otherSymbols = DecimalFormatSymbols()
otherSymbols.setDecimalSeparator(',')
otherSymbols.setGroupingSeparator('.')
DecimalFormat df = DecimalFormat("#.##", otherSymbols)
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)

should do the trick. You need to use decimal format symbols for Europe, so a comma for the decimals and a full stop for the thousands.

An alternative to do it on a locale basis is to use NumberFormat and cast it to a DecimalFormat

val df = NumberFormat.getNumberInstance(currentLocale) as DecimalFormat
df.applyPattern("#.##")
...
df.format(lengthCalc)
geco17
  • 5,152
  • 3
  • 21
  • 38