Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a floating number to fixed width in Python

How do I format a floating number to a fixed width with the following requirements:

  1. Leading zero if n < 1
  2. Add trailing decimal zero(s) to fill up fixed width
  3. Truncate decimal digits past fixed width
  4. Align all decimal points

For example:

% formatter something like '{:06}' numbers = [23.23, 0.123334987, 1, 4.223, 9887.2]  for number in numbers:     print formatter.format(number) 

The output would be like

  23.2300    0.1233    1.0000    4.2230 9887.2000 
like image 974
hobbes3 Avatar asked Jan 16 '12 20:01

hobbes3


People also ask

How do I format a float number in Python?

Format float value using the round() Method in Python The round() is a built-in Python method that returns the floating-point number rounded off to the given digits after the decimal point. You can use the round() method to format the float value.

How do you print fixed width in Python?

To print a string at a fixed width with Python, we can use the string's format method with a format code. to return a string with length 5. The 's' string is padded to length 5 with empty trailing spaces. Therefore, print prints ''s ' .

How do you fix digits in Python?

Format a Floating Number to a Fix Width Using round() Function in Python. We can also use the round() function to fix the numbers of digits after the decimal point. This function limits the number of digits after the decimal point on the input number.

What is %s %d %F in Python?

Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.


1 Answers

numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]                                                                                                                                                                                                                                                                                                                                                     for x in numbers:                                                                                                                                                                                    print("{:10.4f}".format(x))  

prints

   23.2300     0.1233     1.0000     4.2230  9887.2000 

The format specifier inside the curly braces follows the Python format string syntax. Specifically, in this case, it consists of the following parts:

  • The empty string before the colon means "take the next provided argument to format()" – in this case the x as the only argument.
  • The 10.4f part after the colon is the format specification.
  • The f denotes fixed-point notation.
  • The 10 is the total width of the field being printed, lefted-padded by spaces.
  • The 4 is the number of digits after the decimal point.
like image 155
Sven Marnach Avatar answered Sep 18 '22 13:09

Sven Marnach