0

How can I format a floating variable like %6.2f would do in printf (C) and store the result in a string variable?

1

1 Answer 1

3

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|
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.