thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Create decimal fields with precision #3793

Open kamov opened 5 years ago

kamov commented 5 years ago

Version information

Description

If you create a field type decimal, you can't add precision, like 10,8

Steps To Reproduce

Create a field type decimal, if you try to add length to 10,8 it store as 10,0

Expected behavior

Should store decimal precision

salvisb commented 5 years ago

The same issue, would be really useful to fix it.

logixtreeCode commented 4 years ago

+1 facing same issue Any Progress on this one? mid-2020 now and this one is still open...

emptynick commented 4 years ago

https://twitter.com/taylorotwell/status/1256031217943818242

Also, if I remember correctly, this was a problem with DBAL, not Voyager directly.

weidmaster commented 4 years ago

I noticed the same problem with Laravel 7 and latest Voyager. I will take a look on how the table is created because I think it is just a matter of changing the input to allow the comma and for it to be interpreted in the table creating code. As of now, we need to manually update the decimal precision in the database itself, otherwise we can't store monetary values at all.

fletch3555 commented 4 years ago

Monetary values shouldn't be stored in floating-point numbers anyway (https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/. it's a bit old, but the points are still relevant)

weidmaster commented 4 years ago

Monetary values shouldn't be stored in floating-point numbers anyway (https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/. it's a bit old, but the points are still relevant)

That is correct, but the decimal field is not a float. So it works

fletch3555 commented 4 years ago

Yes, but what variable does that value go into when it gets pulled out of the database...?

weidmaster commented 4 years ago

Ah! I get it. Yes, the value will be treated as a float by internal conversion of PHP. But in the actual code you should never round the values, just format it. By the way that just reminded me I need to test saving the value with precision in the database, with the default input number of Voyager

weidmaster commented 4 years ago

Yes, but what variable does that value go into when it gets pulled out of the database...?

I have been thinking and maybe it is our approach that is wrong in this matter. How Voyager wants us to manage decimal or monetary values?

MrCrayon commented 4 years ago

Yes, but what variable does that value go into when it gets pulled out of the database...?

I have been thinking and maybe it is our approach that is wrong in this matter. How Voyager wants us to manage decimal or monetary values?

For what is worth for monetary values I always try to avoid decimals, choose what's the precision and then use that as base. For example if your prices need cents use cents as unit, store the integer value and convert for display.

$ 3.14 stored as 314

weidmaster commented 4 years ago

@MrCrayon oh you are right! I always forget we can do operation with cents and it makes sense to store the value as integer and doing visual transformations for display. Now I just have to know how can I work with currencies that does not have cents at all, like PYG (Paraguay Guaranies). But thanks for the info.

ShenLoong99 commented 3 years ago

I have a amount column in Voyager table and I want to display the values with decimal places and comma (like number_format($var, 2), e.g. 1,000.20). Is it possible?

Stelikas commented 2 years ago

Having the same issue, the number is getting rounded each time i try to save it in the database.