3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Class CRM_Utils_Number
20 class CRM_Utils_Number
{
23 * Create a random number with a given precision.
25 * @param array $precision
26 * (int $significantDigits, int $postDecimalDigits).
30 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
32 public static function createRandomDecimal($precision) {
33 list ($sigFigs, $decFigs) = $precision;
34 $rand = rand(0, pow(10, $sigFigs) - 1);
35 return $rand / pow(10, $decFigs);
39 * Given a number, coerce it to meet the precision requirement. If possible, it should
40 * keep the number as-is. If necessary, this may drop the least-significant digits
41 * and/or move the decimal place.
43 * @param int|float $keyValue
44 * @param array $precision
45 * (int $significantDigits, int $postDecimalDigits).
47 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
49 public static function createTruncatedDecimal($keyValue, $precision) {
50 list ($sigFigs, $decFigs) = $precision;
51 $sign = ($keyValue < 0) ?
'-1' : 1;
52 // ex: -123.456 ==> 123456
53 $val = str_replace('.', '', abs($keyValue));
55 $val = substr($val, 0, $sigFigs);
57 // Move any extra digits after decimal
58 $extraFigs = strlen($val) - ($sigFigs - $decFigs);
61 return $sign * $val / pow(10, $extraFigs);
69 * Some kind of numbery-looky-printy thing.
72 * @param bool $checkForPostMax
76 public static function formatUnitSize($size, $checkForPostMax = FALSE) {
78 $last = strtolower($size[strlen($size) - 1]);
81 // The 'G' modifier is available since PHP 5.1.0
91 if ($checkForPostMax) {
92 $maxImportFileSize = self
::formatUnitSize(ini_get('upload_max_filesize'));
93 $postMaxSize = self
::formatUnitSize(ini_get('post_max_size'));
94 if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) {
95 CRM_Core_Session
::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert");
97 // respect php.ini upload_max_filesize
98 if ($size > $maxImportFileSize && $size !== $postMaxSize) {
99 $size = $maxImportFileSize;
100 CRM_Core_Session
::setStatus(ts("Note: Please verify your configuration for Maximum File Size (in MB) <a href='%1'>Administrator >> System Settings >> Misc</a>. It should support 'upload_max_size' as defined in PHP.ini.Please check with your system administrator.", [1 => CRM_Utils_System
::url('civicrm/admin/setting/misc', 'reset=1')]), ts("Warning"), "alert");
108 * Format number for display according to the current or supplied locale.
110 * Note this should not be used in conjunction with any calls to
111 * replaceCurrencySeparators as this function already does that.
113 * @param string $amount
114 * @param string $locale
117 * @throws \Brick\Money\Exception\UnknownCurrencyException
119 public static function formatLocaleNumeric(string $amount, $locale = NULL): string {
120 $formatter = new \
NumberFormatter($locale ?? CRM_Core_I18n
::getLocale(), NumberFormatter
::DECIMAL
);
121 $formatter->setSymbol(\NumberFormatter
::DECIMAL_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryDecimalPoint
);
122 $formatter->setSymbol(\NumberFormatter
::GROUPING_SEPARATOR_SYMBOL
, CRM_Core_Config
::singleton()->monetaryThousandSeparator
);
123 return $formatter->format($amount);