-2

Is there a way to this simple equation? I'm getting as a product result : 858.2999999999999682365192655

the simple equation is this:

from decimal import Decimal

 x = Decimal(57220) * Decimal(0.03)
 y = x / Decimal(2)
 print(y)

The expected answer should be 858.30 Also tried this but it rounds this off to 858.00

  x = Decimal(57220) * Decimal(0.03)
  y = x / Decimal(2)
  z = round(y, 2)
  print(z)

I'm really sorry I'm just a beginner here in python

BTW I'm using 3.6 version of python

Edited : I'm sorry it should be 57220 :(

Julien
  • 13,986
  • 5
  • 29
  • 53
OMS
  • 1
  • 5

2 Answers2

0

I think what you get as a result makes sense and is not wrong.

57220 * 0.03 / 2 = (572.20 * 3) / 2 = 286.10 * 3 = 858.30

So, the answer you get from the following block of code makes sense as is supposed to be 858.30.

from decimal import Decimal

x = Decimal(57200) * Decimal(0.03)
y = x / Decimal(2)
# if you want to keep two places after the decimal
y = round(y,2)
print(y)

Output:

858.30

EDIT: Updated solution based on OP's feedback about a typo in the question.

CypherX
  • 7,019
  • 3
  • 25
  • 37
0

You don't need Decimal. neither you have to convert to float. Make it simple and it will give exact answer. Thanks!!

x = 57220 * 0.03
y = x / 2
print(y)

Output:

858.3
Sapan Zaveri
  • 490
  • 7
  • 18
  • Correct. Although i need to compare 2 values which need 2 decimal points (comparing money with 2 decimal) although thats a different story – OMS Dec 06 '19 at 05:02