Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's equivalent to C# Hash (#) in a format string

Tags:

python

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.

like image 446
Moataz Elmasry Avatar asked Nov 21 '25 11:11

Moataz Elmasry


1 Answers

try this:

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

Output:

12.355000 0.1
like image 72
MaxU - stop WAR against UA Avatar answered Nov 23 '25 01:11

MaxU - stop WAR against UA