3

In C# and other languages, a hash (#) in the format string will be replaced by a number if one exists, and nothing if it doesn't. So a string such as:

number1 = 12.3456
number2 = 12.3
String.Format("0.00####", number1)
String.Format("0.00####", number2)

Will output number1 = 12.3456 but number2 = 12.30. A zero in the format string means if there isn't enough decimal numbers, a zero will be printed out instead.

My question is, is there a similar functionality in Python? I know I can use "{:.6F}" to format a number to 6 decimal points.

1

1 Answer 1

3

try this:

print("{:0<.6f} {:0<.1f}".format(12.355, 0.12345))

Output:

12.355000 0.1
Sign up to request clarification or add additional context in comments.

3 Comments

When i change it to "{:0<1f}".format(12.355), I still get 12.355000. Update: I think you missed a dot in the middle, should be "{:0<.1f}".format(12.355)
@MoatazElmasry, sure, thanks for the correction! i've updated the answer.
no worries, I've marked your answer as correct. Thanks a lot!

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.