3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2015
34 * Class CRM_Utils_Number
36 class CRM_Utils_Number
{
38 * Create a random number with a given precision.
40 * @param array $precision
41 * (int $significantDigits, int $postDecimalDigits).
45 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
47 public static function createRandomDecimal($precision) {
48 list ($sigFigs, $decFigs) = $precision;
49 $rand = rand(0, pow(10, $sigFigs) - 1);
50 return $rand / pow(10, $decFigs);
54 * Given a number, coerce it to meet the precision requirement. If possible, it should
55 * keep the number as-is. If necessary, this may drop the least-significant digits
56 * and/or move the decimal place.
58 * @param int|float $keyValue
59 * @param array $precision
60 * (int $significantDigits, int $postDecimalDigits).
62 * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
64 public static function createTruncatedDecimal($keyValue, $precision) {
65 list ($sigFigs, $decFigs) = $precision;
66 $sign = ($keyValue < 0) ?
'-1' : 1;
67 $val = str_replace('.', '', abs($keyValue)); // ex: -123.456 ==> 123456
68 $val = substr($val, 0, $sigFigs); // ex: 123456 => 1234
70 // Move any extra digits after decimal
71 $extraFigs = strlen($val) - ($sigFigs - $decFigs);
73 return $sign * $val / pow(10, $extraFigs); // ex: 1234 => 1.234
81 * Some kind of numbery-looky-printy thing.
84 * @param bool $checkForPostMax
88 public static function formatUnitSize($size, $checkForPostMax = FALSE) {
90 $last = strtolower($size{strlen($size) - 1});
92 // The 'G' modifier is available since PHP 5.1.0
102 if ($checkForPostMax) {
103 $maxImportFileSize = self
::formatUnitSize(ini_get('upload_max_filesize'));
104 $postMaxSize = self
::formatUnitSize(ini_get('post_max_size'));
105 if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) {
106 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");
108 // respect php.ini upload_max_filesize
109 if ($size > $maxImportFileSize && $size !== $postMaxSize) {
110 $size = $maxImportFileSize;
111 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.", array(1 => CRM_Utils_System
::url('civicrm/admin/setting/misc', 'reset=1'))), ts("Warning"), "alert");