[Answered ]-How to truncate Django Measurement object to 2 decimal points

1👍

You can work with the round(…) builtin function [python-doc]:

>>> round(Decimal('0.123456789'), 2)
Decimal('0.12')
>>> round(Decimal('0.123456789'), 3)
Decimal('0.123')

You can thus construct a new Decimal by rounding a Decimal with a given number of decimals.

0👍

For conversions, access the preferred unit attribute to get a converted distance quantity:

d1 = Distance(km=5)

print(d1.mi) # Converting 5 kilometers to miles
    3.10685596119
print(d2.km) # Converting 5 miles to kilometers
    8.04672

source Django Docs

Leave a comment