BigBadWolf
BigBadWolf

Reputation: 603

Scala: Print Double with decimal and thousands seperator and 2 decimal places

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!

Upvotes: 1

Views: 2619

Answers (2)

Shyamendra Solanki
Shyamendra Solanki

Reputation: 8851

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

Upvotes: 3

bjfletcher
bjfletcher

Reputation: 11508

println(f"$Double%,.2f") // -> 12,591,294,124.13
println(f"$Double%,.2f"
  .replace(',', '-')
  .replace('.', ',')
  .replace('-', '.')) // -> 12.591.294.124,13

Upvotes: 1

Related Questions