diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 7658750b9957..b5e1c8016aa3 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -1155,20 +1155,20 @@ protected function getCurrency($params = []) { } /** - * Legacy. Better for a method to work on its own PropertyBag, - * but also, this function does not do very much. + * Get the submitted amount, padded to 2 decimal places, if needed. * * @param array $params * * @return string - * @throws \CRM_Core_Exception */ protected function getAmount($params = []) { if (!CRM_Utils_Rule::numeric($params['amount'])) { CRM_Core_Error::deprecatedWarning('Passing Amount value that is not numeric is deprecated please report this in gitlab'); return CRM_Utils_Money::formatUSLocaleNumericRounded(filter_var($params['amount'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION), 2); } - return CRM_Utils_Money::formatUSLocaleNumericRounded($params['amount'], 2); + // Amount is already formatted to a machine-friendly format but may NOT have + // decimal places - eg. it could be 1000.1 so this would return 1000.10. + return Civi::format()->moneyNumber($params['amount'], 'USD', 'en_US'); } /** diff --git a/Civi/Core/Format.php b/Civi/Core/Format.php index b3076d0e8dc6..c9fdc8e524c9 100644 --- a/Civi/Core/Format.php +++ b/Civi/Core/Format.php @@ -93,8 +93,26 @@ public function moneyNumber($amount, string $currency, $locale): string { return ''; } $formatter = $this->getMoneyFormatter($currency, $locale, NumberFormatter::DECIMAL); - $money = Money::of($amount, $currency, NULL, RoundingMode::HALF_UP); - return $money->formatWith($formatter); + return Money::of($amount, $currency, NULL, RoundingMode::HALF_UP)->formatWith($formatter); + } + + /** + * Get a number formatted to a machine format with decimal placed padded by currency. + * + * Most currencies format to 2 decimal places so the default of 'USD' will + * achieve that. + * + * For example an input of 1000.1 will return 1000.10. + * + * This will ensure that + * + * @param string|float|int $amount + * @param string $currency + * + * @return string + */ + public function machineNumber($amount, string $currency = 'USD'): string { + return Civi::format()->moneyNumber($amount, $currency, 'en_US'); } /**