How can I format a floating variable like %6.2f
would do in printf
(C) and store the result in a string
variable?
Here's a floating point ( Double
) value and a handful of String
format options via string interpolation.
val d = 12345.678
f"|$d%f|${-d}%f|" // |12345.678000|-12345.678000|
f"|$d%+f|${-d}%+f|" // |+12345,678000|-12345,678000|
f"|$d% f|${-d}% f|" // | 12345,678000|-12345,678000|
f"|$d%.2f|${-d}%.2f|" // |12345,68|-12345,68|
f"|$d%,.2f|${-d}%,.2f|" // |12,345.68|-12,345.68|
f"|$d%.2f|${-d}%(.2f|" // |12345,68|(12345,68)|
f"|$d%10.2f|${-d}%10.2f|" // | 12345,68| –12345,68|
f"|$d%010.2f|${-d}%010.2f|" // |0012345,68|-012345,68|
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.