Hayden Ravenscroft - 20 days ago 4x

Python Question

I need to calculate the area of any regular polygon. I acquired this formula from a good ol' google:

`(n * l**2) / (4 * math.tan((180/n) * (math.pi/180)))`

No matter how I use

`decimal.Decimal`

`n`

`l`

Can someone apply decimal object to this please? When

`n`

`l`

`4`

`4.00000000000000156234`

Answer

You need to set the precision for the decimal object. Pi is an infinite decimal number, so the precision won't be a whole number.

You can do

```
from decimal import *
getcontext().prec = 1
```

or add to the decimal object by rounding down.

```
>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32')
```

Source (Stackoverflow)

Comments