Merge pull request #22053 from civicrm/5.44
[civicrm-core.git] / CRM / Utils / Number.php
index ff254735958b65660c0c0ffb404c9fdb3b7385f7..790fdd1278e233b90f54f94591226b5e065c0a80 100644 (file)
@@ -75,7 +75,7 @@ class CRM_Utils_Number {
    */
   public static function formatUnitSize($size, $checkForPostMax = FALSE) {
     if ($size) {
-      $last = strtolower($size{strlen($size) - 1});
+      $last = strtolower($size[strlen($size) - 1]);
       $size = (int) $size;
       switch ($last) {
         // The 'G' modifier is available since PHP 5.1.0
@@ -104,4 +104,23 @@ class CRM_Utils_Number {
     }
   }
 
+  /**
+   * Format number for display according to the current or supplied locale.
+   *
+   * Note this should not be used in conjunction with any calls to
+   * replaceCurrencySeparators as this function already does that.
+   *
+   * @param string $amount
+   * @param string $locale
+   *
+   * @return string
+   * @throws \Brick\Money\Exception\UnknownCurrencyException
+   */
+  public static function formatLocaleNumeric(string $amount, $locale = NULL): string {
+    $formatter = new \NumberFormatter($locale ?? CRM_Core_I18n::getLocale(), NumberFormatter::DECIMAL);
+    $formatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, CRM_Core_Config::singleton()->monetaryDecimalPoint);
+    $formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, CRM_Core_Config::singleton()->monetaryThousandSeparator);
+    return $formatter->format($amount);
+  }
+
 }