I'm trying to compose a formatted string that should look like this:
A bottle of vodka costs x.yz rubles.
where x.yz is an arbitrary Double value rounded to 2 decimal signs.
To achieve this, I use string interpolation with printf syntax:
val cost = 2.56
val str = f"A bottle of vodka costs $cost%.2f rubles."
Unfortunately, on a machine with Russian locale, the snippet above yields a result that is a bit different from what I want to achieve:
str: String = A bottle of vodka costs 2,56 rubles.
This is understandable (the Java formatter used by f interpolator applies the locale-specific decimal separator, as outlined here ), but I still want to use decimal dot instead of comma.
I'm aware of a possible workaround – using the formatLocal()
method with explicitly set locale:
val str = "A bottle of vodka costs %.2f rubles.".formatLocal(java.util.Locale.US, cost)
but it does not strike me as a particularly Scalaesque way of doing things.
So my question is this: is it possible to format a string with a specific decimal separator using only f interpolation, without any extraneous method calls?
目前, f
插补器不支持语言环境或自定义分隔符(请参阅macro_StringInterpolation_f.scala
/ FastTrack.scala
)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.