1

I am trying to print a double like this:

val Double = 12591294124.125124
printf("%,2f", Double)

with the wanted output: 12.591.294.124,13

Oddly enough, when using "%,2f" it doesn't round to 2 decimal places, but prints 6 like this: 12.591.294.124,125124 Is there a way to use the , as decimal seperator and round to 2 digits? thanks!

BigBadWolf
  • 603
  • 2
  • 8
  • 17
  • `printf("%,.2f", Double)`, but the grouping and decimal symbol are locale dependent, if this is not what you want, you can look at this [question](http://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point). – Peter Neyens Jun 27 '15 at 09:01

2 Answers2

3

You may choose Belgium locale to get this representation:

import java.util.Locale._

val belgiumLocale = getAvailableLocales.filter(_.getDisplayCountry == "Belgium")
                                       .head

val Double = 12591294124.125124
"%,.2f".formatLocal(belgiumLocale, Double)
// 12.591.294.124,13
Shyamendra Solanki
  • 8,751
  • 2
  • 31
  • 25
1
println(f"$Double%,.2f") // -> 12,591,294,124.13
println(f"$Double%,.2f"
  .replace(',', '-')
  .replace('.', ',')
  .replace('-', '.')) // -> 12.591.294.124,13
bjfletcher
  • 11,168
  • 4
  • 52
  • 67