Skip to content

Commit 1b683ae

Browse files
committed
Implemented Decimal to properly format currency numbers, especially useful for 0 values, because floats do not print 0.0 as 0.00, as required by this standard.
1 parent 4afaf6e commit 1b683ae

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

PyFePA/fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import datetime
1919
import importlib
2020
import dateutil.parser
21+
from decimal import Decimal
2122

2223

2324
class FieldType(object):
@@ -141,7 +142,7 @@ def validate(self, value):
141142
if super(FieldDecimal,self).validate(value):
142143
return value
143144
elif self.minlen <= len('{:.2f}'.format(float(value))) <= self.maxlen:
144-
return float(value)
145+
return Decimal(value).quantize(Decimal('.01'))
145146
except(ValueError, TypeError):
146147
print('DEBUG- ', value)
147148
return False
@@ -155,7 +156,7 @@ def __init__(self, **kwargs):
155156

156157
@classmethod
157158
def tostring(cls,value):
158-
return '{:.2f}'.format(float(value))
159+
return unicode(Decimal(value).quantize(Decimal('.01')))
159160

160161

161162
class FieldDate(FieldType):

0 commit comments

Comments
 (0)