# [Tutor] Is there anyway to set how many numbers are used after the decimal in floating numbers?

Nathan Pinno falcon3166 at hotmail.com
Thu Aug 18 19:17:42 CEST 2005

```I want to use it for a currency exchange program. It's useful, then the user
wouldn't have to round it himself/herself, and mistakes could be avoided.

Nathan Pinno
---------------------------------------------------------------
Early to bed,
Early to rise,
Makes a man healthy, wealthy, and wise.
--Benjamin Franklin
-------------------------------------------------------------------
Languages I know: Python, English
Languages I am learning: C++, Java, Javascript
----- Original Message -----
From: "Bob Gailer" <bgailer at alum.rpi.edu>
To: "Nathan Pinno" <falcon3166 at hotmail.com>; "Tutor mailing list"
<tutor at python.org>
Sent: Thursday, August 18, 2005 11:11 AM
Subject: Re: [Tutor] Is there anyway to set how many numbers are used after
the decimal in floating numbers?

> At 10:31 AM 8/18/2005, Nathan Pinno wrote:
>>Is there anyway to set how many numbers are used after the decimal in
>>floating numbers? It would be nice if the answer could be rounded to 2
>>decimal spots, instead of the ten millionths spot.
>
> The simple answer is NO. Floating point numbers have a "fixed" number of
> digits and an exponent to place the decimal point. You can control how
> many digits are "displayed" using % formatting.
> >>> 5./3
> 1.6666666666666667
> >>> '%6.3f' % (5./3) # notice this rounds and formats
> ' 1.667'
>
> There are various other ways to accomplish your goals depending on what
> they are. What are you using floating point numbers for? It is easy to get
> into trouble with floating point arithmetic since (1) most decimal numbers
> do not have an exact floating point representation, and (2) the limited
> precision can caus cumulative errors. Example:
> >>> i = 0.0
> >>> for j in range(10):i += j*.1
> >>> i
> 4.5000000000000009
>
> If you have Python 2.4 + the decimal module may give you what you need.
>
> Bob Gailer
> 303 442 2625 home
> 720 938 2625 cell
>
```