[Tutor] Floating decimal question

Alan Gauld alan.gauld at yahoo.co.uk
Mon Dec 18 19:45:55 EST 2017

On 18/12/17 22:09, Roger Lea Scherer wrote:

> bill = float(input("What is the price of the bill?: "))
> tip15 = bill*1.15
> tip20 = bill*1.20
> print("Bill plus 15% gratuity is " + str(tip15))
> print("Bill plus 20% gratuity is " + str(tip20))
> This is the result

> Bill plus 15% gratuity is 34.488499999999995
> Bill plus 20% gratuity is 35.988
> My question is why does the 15% gratuity go so far beyond the decimal place
> when really there should only be 4 places because of multiplication rules,

Remember this is binary arithmetic not decimal.
The number of decimal points is not something the
computer sees.

Also, when working with monetary values you should get
into the habit of either using the Decimal module
or converting to pennies, although that still
won't help if multiplying by fractions - you'd
need to convert the percents into full figures too.

Look up floating point accuracy on Wikipedia they
have a good explanation of what you are seeing.


Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list