[Tutor] How can I let the Python Console display more decimal precision?

James Chapman james at uplinkzero.com
Tue Jul 1 12:56:12 CEST 2014


You could just initialise your variables with float()

>>> float(26)/float(12)
2.1666666666666665

>>> varA = float(26)
>>> varB = float(12)
>>> varA/varB
2.1666666666666665

And so on...

In fact, you only need to initialise one variable with float for this to
work:

>>> varA = float(26)
>>> varB = 12
>>> varA/varB
2.1666666666666665

This works in Python2 or Python3 without importing any extra libs.




--
James


On 12 June 2014 13:31, Steven D'Aprano <steve at pearwood.info> wrote:

> On Thu, Jun 12, 2014 at 08:48:25AM +0800, Marino David wrote:
> > Hi All:
> >
> > I am a newbie at the Python.
> >
> > I type "26/12" in Python Console and get result of "2".
> >
> > It is obvious that the corresponding result should be 2.3333...... I
> don't
> > know why the Console only returns the integer part of  true result.
> Anyone
> > can help me out?
>
> Try this instead:
>
> 26.0/12
>
> and it will print a fractional number instead of an int:
>
> py> 26.0/12
> 2.1666666666666665
>
>
> What's going on?
>
> Back in the early mists of time, when Python first came out, Python's
> creator Guido van Rossum decided that the / division operator should
> behave like in the C programming language. In C, division of two
> integers performs *integer division*, and drops the remainder, while
> division of one or more floating point number keeps the remainder as a
> fraction:
>
> 1/2 => 0
> 1/2.0 => 0.5
>
> That was one of those decisions that seemed like a good idea at the
> time, but turned out to be a mistake. But for backwards compatibility,
> Python had to keep it until recently.
>
> In Python version 3, / now does calculator division, like you expect.
> But in Python 2, you have to either convert one or both numbers to a
> float, or you can put this at the top of your program:
>
> from __future__ import division
>
> Note that there are TWO underscores at the beginning and end of
> "future".
>
>
> If you want integer division, where the remainder is ignored, you can
> use the // operator:
>
> py> 26.0//12
> 2.0
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140701/536e719e/attachment.html>


More information about the Tutor mailing list