mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Handle float values
This commit is contained in:
parent
486d2bb51b
commit
f6ebd0240e
@ -1,4 +1,4 @@
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
@ -42,10 +42,11 @@ def to_meters(length, unit) -> Decimal:
|
||||
Convert the given length to meters, returning a Decimal value.
|
||||
"""
|
||||
try:
|
||||
length = Decimal(length)
|
||||
except InvalidOperation:
|
||||
raise TypeError(_("Invalid value '{length}' for length (must be a number)").format(length=length))
|
||||
if length < 0:
|
||||
raise ValueError(_("Length must be a positive number"))
|
||||
except TypeError:
|
||||
raise TypeError(_("Invalid value '{length}' for length (must be a number)").format(length=length))
|
||||
|
||||
if unit == CableLengthUnitChoices.UNIT_KILOMETER:
|
||||
return round(Decimal(length * 1000), 4)
|
||||
|
@ -28,8 +28,8 @@ class ConversionsTest(TestCase):
|
||||
|
||||
def test_to_meters(self):
|
||||
self.assertEqual(
|
||||
to_meters(1, CableLengthUnitChoices.UNIT_KILOMETER),
|
||||
Decimal('1000')
|
||||
to_meters(1.5, CableLengthUnitChoices.UNIT_KILOMETER),
|
||||
Decimal('1500')
|
||||
)
|
||||
self.assertEqual(
|
||||
to_meters(1, CableLengthUnitChoices.UNIT_METER),
|
||||
|
Loading…
Reference in New Issue
Block a user