Based on this year 2008 thread and a few earlier threads on civi.stackexchange regarding the matter. Now (CiviCRM 5.56.2), when "Monetary Amount Display" on civicrm/admin/setting/localization?reset=1 is deprecated and gone, how do we, say, remove decimals from currency display, apart from altering CRM_Utils_Money
?
-
1Same interest in altering: lab.civicrm.org/dev/core/-/issues/1494#note_65518– Andy BurnsCommented Jan 6, 2023 at 16:53
1 Answer
While not a supported option, theoretically you can replace \Civi::$statics[$cacheKey];
from https://github.com/civicrm/civicrm-core/blob/b0fff1c64cf3dd2e40b248b3b067ca30de09aeee/Civi/Core/Format.php#L220, probably in hook_civicrm_config in an extension, with a modified $formatter object that does what you want.
e.g. in hook_civicrm_config
$cacheKey = <figure out cachekey>
$formatter = \Civi::$statics[$cacheKey];
// make some changes to $formatter here
\Civi::$statics[$cacheKey] = $formatter;
But it would be nicer if there was a supported way to change the parameters that the formatter object uses.
-
Wouldn't it be easier to have a copy of Money.php in custom php directory and alter getCurrencyPrecision() ?– yurgCommented Jan 6, 2023 at 21:21
-
It makes upgrades harder if you need to sync changes every time, but yes that should work too.– DemeritCommented Jan 6, 2023 at 21:39
-
Indeed; I was thinking about a tiny extension which would extend the CRM_Utils_Money class, just not sure which hook to use.– yurgCommented Jan 6, 2023 at 22:58
-
1If we assume the target audience for customizing is developers, then I think the solution to that would be to change this line in core so that it acts like a service, similar to the logger, so that you could replace it like the way monolog does.– DemeritCommented Jan 6, 2023 at 23:33
-
That would replace the entire Format class not just the money formatter, but it's the same idea, you'd extend it, and just override the part you need.– DemeritCommented Jan 6, 2023 at 23:34