Skip to content
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

Set 4 Decimal points to be saved in the Database #61

Open
hizmarck opened this issue Oct 9, 2024 · 2 comments
Open

Set 4 Decimal points to be saved in the Database #61

hizmarck opened this issue Oct 9, 2024 · 2 comments
Milestone

Comments

@hizmarck
Copy link

hizmarck commented Oct 9, 2024

Describe the bug
I have the next use case, which is to save 4 decimal digits:

Steps To Reproduce

MoneyInput::make('total_cents')
                            ->decimals(4)

.env

MONEY_DEFAULT_LOCALE=es_MX
MONEY_DEFAULT_CURRENCY=MXN
MONEY_DECIMAL_DIGITS=4

composer.json

"php": "^8.2",
"laravel/framework": "^11.9",
"filament/filament": "^3.2",
"pelmered/filament-money-field": "^1.4",

If I insert this value = 19.5455, the saved one is 19.5500

Expected behavior and actual behavior
Let the MoneyInput save the configured decimal digits.

Additional context
I debugged the code a little and found that the MoneyFormatter use the $moneyParser = new IntlLocalizedDecimalParser($numberFormatter, new ISOCurrencies);

in which we have 2 minor units:

'MXN' =>
  array (
    'alphabeticCode' => 'MXN',
    'currency' => 'Mexican Peso',
    'minorUnit' => 2,
    'numericCode' => 484,
  ),

I can understand that for Money calculations this configuration is correct and that at the end we need to round the final changes, so maybe this is not a bug, but for my use case is almost impossible to find a solution, ty very much.

@pelmered
Copy link
Owner

Thanks for the report!
Sorry for the late reply. I've been very busy the last month.

I was able to reproduce this. I looks like the number formatted gets properly configured with the MONEY_DECIMAL_DIGITS var, but IntlLocalizedDecimalParser does not respect that.

The problem is in IntlLocalizedDecimalParser as you said where it does not allow for more decimals then the minor unit for that currency. The only solution I can think of to fix this would be to allow that you inject your own Decimal parser that implements Money\MoneyParser and replaces IntlLocalizedDecimalParser.

Is that something you would want?

@hizmarck
Copy link
Author

Hi @pelmered, ty for your response. That sure will work for me.

I also was thinking of a way only to inject the currencies:

new CurrencyList([ 'MXN' => 4, // minor unit pairs 'USD' => 2 // minor unit pairs ])

instead of the ISOCurrencies.

new IntlLocalizedDecimalParser($numberFormatter, new ISOCurrencies);

But, reading your response, I can see better control if we can implement our own Money\MoneyParser.

Regards.

@pelmered pelmered added this to the 2.0 milestone Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants