Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align positive and negative values along the decimal point in python

I want to write bunch of positive and negative values into a text file as below

1      12.203026     2.291063     -0.061603
1      -0.736147     1.353548     -1.347932
6       0.728048     1.348907      0.247566
6      -0.728048     1.348907    -10.247565
1      10.736147     1.353547      1.347932
1      -1.203026    12.291063      0.061604
6      11.583889     0.179143    -10.258947

when I am writing that it should be aligned along the decimal points as above. The code I written using following f.write() command and it's out put as below.

 f.write('%s    %2.6f   %2.6f   %2.6f\n' % (a, x, y, z ))

out put

1      12.203026    2.291063    -0.061603
1      -0.736147    1.353548    -1.347932
6      0.728048     1.348907    0.247566
6      -0.728048    1.348907    -10.247565
1      10.736147    1.353547    1.347932
1      -1.203026    12.291063    0.061604
6      11.583889    0.179143    -10.258947
like image 633
user3817989 Avatar asked Mar 11 '15 01:03

user3817989


People also ask

How do you align decimals in Python?

You can use the :> , :< or :^ option in the f-format to left align, right align or center align the text that you want to format. We can use the fortmat() string function in python to output the desired text in the order we want.

How do you format a negative number in Python?

Format Signed NumbersOnly negative numbers are prefixed with a sign by default. You can change this by specifying the sign format option. When you use ' ' (space) for sign option, it displays a leading space for positive numbers and a minus sign for negative numbers.

How do you print numbers in exponential format in Python?

To print a number in scientific notation in Python, we use str. format() to print a number in its scientific form in python.

Can negative numbers be integers in Python?

An integer, commonly abbreviated to int, is a whole number (positive, negative, or zero). So 7 , 0 , -11 , 2 , and 5 are integers. 3.14159 , 0.0001 , 11.11111 , and even 2.0 are not integers, they are floats in Python.


2 Answers

The first number in the format is the total number of characters, not the number before the decimal point. When that number is too small it compensates by dropping all the padding. To get what you want you need to use %10.6f, not %2.6f. That's 1 for the -, 2 for the initial digits, 1 for the decimal point, and 6 digits after the decimal - 1+2+1+6=10.

like image 64
Mark Ransom Avatar answered Oct 22 '22 22:10

Mark Ransom


Suppose I have:

>>> li=[['1', 12.203026, 2.291063, -0.061603], ['1', -0.736147, 1.353548, -1.347932], ['6', 0.728048, 1.348907, 0.247566], ['6', -0.728048, 1.348907, -10.247565], ['1', 10.736147, 1.353547, 1.347932], ['1', -1.203026, 12.291063, 0.061604], ['6', 11.583889, 0.179143, -10.258947]]

You can right align into a fixed field width in two steps:

for a,x,y,z in li:
    x1, y1, z1=['{:2.6f}'.format(e) for e in (x,y,z)]
    print '{} {:>13} {:>13} {:>13}'.format(a, x1, y1, z1 )

Prints:

1     12.203026      2.291063     -0.061603
1     -0.736147      1.353548     -1.347932
6      0.728048      1.348907      0.247566
6     -0.728048      1.348907    -10.247565
1     10.736147      1.353547      1.347932
1     -1.203026     12.291063      0.061604
6     11.583889      0.179143    -10.258947

If you want to specify width and precision in one place, you can do:

'{} {:{w}.{p}f} {:{w}.{p}f} {:{w}.{p}f}'.format(a, x, y, z, w=13, p=6 )
like image 37
dawg Avatar answered Oct 22 '22 20:10

dawg