Add a validate callback to Currency objects that checks whether an amount is valid for a currency, e.g. has the right amount of decimals, or, in case of the Malagasy ariary, for instance, which is not divided into 100, but 5 subunits, has decimals that represent a valid fraction of the currency.

CommentFileSizeAuthor
#5 currency_1372414_00.patch6.4 KBxano

Comments

xano’s picture

Perhaps we should change this into a method for rounding amounts based on their currency.

xano’s picture

Title: Add amount validators per currency » Add rounding functions

We should support higher precisions than currencies officially have to prevent rounding errors. However, we should have functions to round amounts to currencies' official precisions.

xano’s picture

  1. Update bartfeenstra/currency so it no longer offers currencies' minor units, but the number of their subunits, which is much more valuable information. Using this information, we can both calculate the number of decimals, and round amounts.
  2. Currency.module will get a new property to override the rounding, which defaults to the number of subunits. As this is (semi-)application-specific, this does not belong in bartfeenstra/currency, but in Currency.module.
xano’s picture

bartfeenstra/currency has been updated. When #1903748: Composer update is fixed, we can use the new subunits property for rounding.

xano’s picture

Title: Add rounding functions » Add rounding configuration
Assigned: Unassigned » xano
Status: Active » Needs review
StatusFileSize
new6.4 KB
  1. Rounding steps can be configured per currency, similar to how Drupal Commerce works.
  2. If no rounding step is configured, default to rounding to one subunit.
  3. The amount Views handler can optionally round amounts.
  4. One issue, that is not really a bug, is that most CLDR number patterns require two decimals. If amounts are rounded before displaying them using any of those patterns, users may perceive precision loss. This only happens for specific combinations of number patterns, currencies, and amounts.
xano’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

xano’s picture

Assigned: xano » Unassigned