0

When storing a currency value as integer (i.e. Cent), is there a way to display it in list view as Euro/Dollar?

Example: € 900 are stored in the database as 90000. EasyAdmin displays this as 90,000. What I'd like to have is 900 (or 900,00 or 900.00).

In EasyAdmin's form view, you can configure this via:

form:
    fields:
        - { property: 'centAmount', type: 'money', type_options: { divisor: 100, currency: 'EUR' } }
Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99

2 Answers2

1

MoneyType is not supported in list view, see https://github.com/EasyCorp/EasyAdminBundle/issues/1995

Here's the workaround outlined at https://github.com/EasyCorp/EasyAdminBundle/issues/1995#issuecomment-356717049 :

  1. Create a file /templates/easy_admin/money.html.twig with:

    {% if value is null %}
        {{ include(entity_config.templates.label_null) }} {# otherwise `null` will be displayed as `0,00 €` #}
    {% else %}
        {{ (value/100)|number_format(2, ',', '.') }} € {# you can omit the `number_format()` filter if you don’t want the cents to be shown #}
    {% endif %}
    

    See /vendor/easycorp/easyadmin-bundle/src/Resources/views/default/field_integer.html.twig for the template that EasyAdmin uses by default.

  2. Activate your new template in easy_admin.yaml:

    list:
        fields:
            - {property: 'centAmount', template: 'easy_admin/money.html.twig' }
    

Result: 900,00 €

Reference: https://symfony.com/doc/current/bundles/EasyAdminBundle/book/list-search-show-configuration.html#rendering-entity-properties-with-custom-templates

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
  • You can also use type_options values: `{{ (value/field_options.type_options.divisor)|number_format(2, ',', '.') }} {{ field_options.type_options.currency }}` – prossel Dec 03 '21 at 17:09
1

I had the same issue with the MoneyField (Easy Admin 3).

This is how I fixed it:

MoneyField::new('price','Prix')
    ->setCurrency('EUR')
    ->setCustomOption('storedAsCents', false);
moussare
  • 11
  • 2