Paul
Paul

Reputation: 205

Printing numbers in python

I have a script that generates some numbers (specifically times in epoch form).

Everytime it generates a number, I append the number to an array (called VALUES) and print both the array and that number. However, the number does not contain as many places after the decimal as the number in the array.

For example, a sample output will look like this (after 3 iterations):

VALUES = [733948.45278935181, 733948.45280092594, 733948.45280092594]
Number = 733948.452801

The third number in the array corresponds to the value in Number.

How come they contain different number of positions after the decimal?

Off-topic: What are the numbers after the decimal called? I thought there was some mathematical term for them I just can't remember what it is.

Note: Code was in python.

Upvotes: 13

Views: 46262

Answers (4)

Zduff
Zduff

Reputation: 171

The part of the number to the right of the decimal point (or more generally, the radix point) is called the fractional part of the number (as opposed to integer part). For the decimal system it can be called the decimal part. In binary I've seen that part of the number referred to as the fractional bits.

From time to time an author will use the term mantissa when referring to that part of a number, but I believe that usage is considered wrong by many, as mantissa -- in my experience -- is most often often used as a synonym to significand.

Upvotes: 1

GreenMatt
GreenMatt

Reputation: 18570

To get the precision you want, you need something like:

VALUES = [733948.45278935181, 733948.45280092594, 733948.45280092594]
for v in VALUES:
    print '%18.11f' % (v)

And the term I've always used for those digits after the decimal point is "those digits after the decimal point". :-)

Upvotes: 1

zvone
zvone

Reputation: 19332

When you print the list, it prints repr(x) for each x in the list.

When you print the number, it prints str(x).

For example:

>>> print 123456789.987654321
123456789.988

>>> print [123456789.987654321]
[123456789.98765433]

>>> print str(123456789.987654321)
123456789.988

>>> print repr(123456789.987654321)
123456789.98765433

Upvotes: 4

Donald Miner
Donald Miner

Reputation: 39893

When python prints out a number, it sometimes prints out more decimal places based on whether the internal method is calling repr or str (which both convert the number to a string). repr will return more decimal places, while str does not.

print calls str, so when you do print Number, it will trim it a tad. However, when you convert a list of numbers to a string when you do print VALUES, the list will internally use repr, giving it more decimal places.

In summary, depending on how it is being printed out, you will see different amounts of decimal places. However, internally, it is still the same number.

If you want to force it to use a specific number of decimal places, you can do this:

print "%.3f" % 3.1415   # prints 3.142

Upvotes: 11

Related Questions