Default Number of Decimal Places to Output in PHP

Asked
Active3 hr before
Viewed126 times

6 Answers

decimalnumberdefault
90%

Meta Stack Overflow , Stack Overflow Public questions & answers , Stack Overflow help chat , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

And when you can't rely on the PHP configuration, don't forget about number_format() which you can use to define how a number is returned, ex:

// displays 3.14 as 3 and 4.00 as 4    
print number_format($price, 0);
// display 4 as 4.00 and 1234.56 as 1,234.56 aka money style
print number_format($int, 2, ".", ",");
load more v
88%

Formats a number with grouped thousands and optionally decimal digits. ,number_format — Format a number with grouped thousands, Sets the number of decimal digits. If 0, the decimal_separator is omitted from the return value. , For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. The following example demonstrates various ways to format a number:

load more v
72%

You should use the number_format() method if two decimal places are needed for all numbers including integers.,When the original number has fewer decimal digits than the decimal places to be formatted, round() function will not add zeros at the end of the number.,Use number_format() Function to Show a Number to Two Decimal Places in PHP,Using number_format() function

The built-in function number_format() is used to format a number. By formatting, we mean that the number is displayed with a decimal point and a thousand separator. It also rounds a number if needed. We can use this function to show a number to two decimal places. The correct syntax to use this function is as follows

number_format($number, $NumOfDecimals, $decimalIndicator, $thousandSeparator)
load more v
65%

return number_format((float) $number, 2, '.', '');
load more v
75%

Number of decimal places to use, ie. 2,Maximum number of decimal places to use, ie. 2,Maximal number of decimal places to use, ie. 2,Number Formatting Currency Values Setting the Default Currency Formatting Floating Point Numbers Formatting Percentages Interacting with Human Readable Values Formatting Numbers Format Differences Configure formatters

namespace App\ Controller;

use Cake\ I18n\ Number;

class UsersController extends AppController {
   public
   function initialize() {
      parent::initialize();
      $this - > loadComponent('Auth');
   }

   public
   function afterLogin() {
      $storageUsed = $this - > Auth - > user('storage_used');
      if ($storageUsed > 5000000) {
         // Notify users of quota
         $this - > Flash - > success(__('You are using {0} storage', Number::toReadableSize($storageUsed)));
      }
   }
}
load more v
40%

$decimalpoint: It is optional parameter and used to specifies the string to use for the decimal point.,$decimals: It is optional parameter and used to specifies decimals. If this parameter is set, the number will be formatted with a dot (.) as the decimal point.,$number: It is required parameter which specified the number to be formatted. If no other parameters are set, the number will be formatted without decimals and with the comma (, ) as the thousands separator.,$sep: It is optional parameter and used to specifies string to use for thousands separator. If this parameter is given, then all other parameters are required.

Syntax:

string number_format($number, $decimals, $decimalpoint, $sep)

Input: $number = 100000
Output: 10, 000

Input: $number = 10000
$decimals = 3
$decimalpoints = "."
$sep = ,
   Output: 10, 0000.000
Output:
999, 999
999, 999.490
10, 000, 000
9, 999, 999.990
1 GGG000GGG000 #990
Output
PHP Warning: number_format() expects parameter 1 to be float,
   string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 5

PHP Warning: number_format() expects parameter 1 to be float,
   string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 8
Output
PHP Warning: Wrong parameter count
for number_format() in /home/e426108b066d9a86366249bf7b626d19.php on line 6
load more v

Other "decimal-number" queries related to "Default Number of Decimal Places to Output in PHP"