0

I have a basic program which I'm using to calculate the compound interest of a value, but when I return the value, it is correct but the program has added on more decimal places. Here's what the calculation look like if the principle (200) was increased by a rate (10%) for some time (3 years):

principle = float(input("Enter principle value: ")) #suppose you input 200
rate = float(input("Enter rate: ")) #suppose you input 30
time = float(input("Enter time: ")) # suppose you input 2
interest = (1+(rate/100))**time #This is the formula for calculating the interest
new_value = principle*interest
print (new_value)

This ends up printing: 266.2000000000001, the answer is 266.2 which the program has calculated, but why is it adding the 000000000001 after the answer. I have tried f-string formatting but that rounds the value to a decimal point. In this case, any other inputs would give different decimal points, and using f-string formatting would prevent all the other values from being presented which one might require. How can I output/store the exact and correct value from these calculations, without it returning added binary values?

Jumanji176
  • 47
  • 9
  • 1
    Problems with [floating-point arithmetics](https://docs.python.org/3/tutorial/floatingpoint.html) are nothing new! – Nguyen Thai Binh Mar 21 '21 at 12:01
  • You need to round the floating point valoue to two decimal places i.e. print (f"{new_value:.2f}") to get your desired result. – DarrylG Mar 21 '21 at 12:02

2 Answers2

0

@jumanji176 It's looks like a floating-point-arithmatics as suggested by @Nguyen.

You need to round it up to 2 digits to solve an issue.

round(value, 2)
Dhruv Rajkotia
  • 370
  • 2
  • 8
0

Some people already mentioned how to fix. But you also asked why?

Variables have types in Python (and in most languages). Eg. "str" is a type. "int" (2,3,4) is another. "float" (2.0, 3.0, 4.0) is another. Python is dynamically typed which means it tries to be smart.

Any time you divide (eg. interest = (1+(rate/100))**time), you're going to get a float back. Even trying 8/2 will give you "4.0"

With computers, dividing is actually not straightforward. (Other people provided links for further reading.) But in short: You will get weird, unintuitive precision errors, sometimes.

If you're outputting to a string, there are multiple ways to format. The other answers work, but I also suggest looking up "f-string formatting" which is very powerful and helpful. It can do all kinds of things like setting precision, padding, adding delimiter separators (like commas). Basically, a string is anything you want to be formatted!

In your case, try: print(f"{new_value:.1f}") to get 266.2.

Edit: This is a good f-strings "cookbook": https://mkaz.blog/code/python-string-format-cookbook/

Edit2: With money though, if you care about pennies, you can do:
print(f"{new_value:.2f}") ➡️ 266.20
or if you don't care about cents at all:
print(f"{new_value:.0f}") ➡️ 266

Robert Lin
  • 369
  • 3
  • 9
  • Yes, this can work, but what if it has more decimal places which can change with different values? – Jumanji176 Mar 21 '21 at 19:09
  • @Jumanji176 -- I don't think I understand your question. By using an `f-string`, you are telling Python that you **always** want a number printed to a certain precision as a string. Eg `.2f` will always gives you A.BC. `.1f` will always give you A.B. And `.0f` will always give you A. Does that make sense? – Robert Lin Mar 22 '21 at 02:04
  • That makes sense. The reason I asked this question is because I'm working on a basic calculator program, where I would like to return the exact calculator values of calculation. This works, but it rounds off the final value to two decimal points or one, although values can have ranging decimal points. Thus, this would not work as you do not get the precise calculator value which I require, not a rounded off digit. – Jumanji176 Mar 22 '21 at 06:08