-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need Decimal Property for accounting/costing/monetary applications #235
Comments
Hi, glad you are enjoying neomodel!
I would suggest subclassing Property to use the decimal module. I will have
a look at adding it to neomodel 3 also. In the meantime I strongly suggest
you upgrade there are lots of important fixes in neomodel 3.1 and lots of
nice features in neo4j 3.
…On 6 Feb 2017 14:14, "Swapnil Sawant" ***@***.***> wrote:
Hi There, Thanks for an awesome library and very helpful to us.
I am using neomodel==2.0.2 and neo4j 2.2.9 to create a monetary
application. I am using FloatProperty but facing the issues related to
using floats <https://docs.python.org/3/tutorial/floatingpoint.html>.
Cost doesn't tally.
I am not using any round, format or any other functions. Python advocates using
decimals for such applications
<https://docs.python.org/3/library/decimal.html>
As I saw currently new properties were added like Email, Normal and Regex,
This one can be helpful too.
It would be very helpful, if you suggest any workaround/trick to do this
in neomodel 2.0.2
Thanks
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#235>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAJ0YSMx7eg33HL8vdMRKa2UBFpOqWcsks5rZxzIgaJpZM4L4K3j>
.
|
I think something like this should do the trick. Unless you are wanting to limit the precision of the stored values? class DecimalProperty(Property):
"""
Stores a python decimal.Decimal value
"""
form_field_class = 'FloatField'
@validator
def inflate(self, value):
return decimal.Decimal(value)
@validator
def deflate(self, value):
return decimal.Decimal(value)
def default_value(self):
return decimal.Decimal(super(DecimalProperty, self).default_value()) |
Hi, Thanks for quick response. We started this app with neo4j 3.0, but neomodel wasn't supporting it back then, so had to fall back to 2.x. As soon as we finish this sprint, we will upgrade as per your suggestion
I will try this subclassing property trick and will let you know. Thanks. Really Appreciated. |
I actually wrote a refund system earlier in my career and made the mistake of representing the cent as decimals. The rounding caused all kinds of issues. I think you are better off storing the cent as a full integer then moving the decimal point. For example 5.01 would be stored as 501 and just formatted when displayed. However if you do want to persist to a certain level of decimal places you just need to add a call to round() in the deflate method above and possible inflate for consistency. I think you will still need to call getcontext() to set the precision though. |
I tried subclassing property trick and it works, and found same issues as you did. Even round(decimal.Decimal(value),2) and decimal.Decimal(format(value, '7.2f')) are not able to limit decimals to 2 for our calculations. We are thinking of using the full Integer technique to store costs. Thanks for your guidance. |
@divinedeveloper Would a seperate "decimals" property still be of interest here? In addition to @robinedwards suggestion, another thing that could be done purely on
Where:
This produces:
All the best |
@aanastasiou I believe a decimal property would be helpful but for our scenario we made it work using full integers. Thanks |
@divinedeveloper Thanks for responding, sounds good, a |
Hi There, Thanks for an awesome library and very helpful to us.
I am using python 2.7.9, django 1.8.9, neomodel 2.0.2 and neo4j 2.2.9 to create a monetary application. I am using FloatProperty but facing the issues related to using floats. Cost doesn't tally.
I am not using any round, format or any other functions. Python advocates using decimals for such applications
As I saw currently new properties were added like Email, Normal and Regex, This one can be helpful too.
It would be very helpful, if you suggest any workaround/trick to do this in neomodel 2.0.2
Thanks
The text was updated successfully, but these errors were encountered: