Several methods of preserving two decimal places in PHP

  • 2021-12-13 16:27:44
  • OfStack

The code looks like this:


$num = 10.4567;  
  // No. 1 1 Species: Utilization round() On floating-point numbers 4 Shed 5 Into 
  echo round($num,2); //10.46
   
  // No. 1 2 Species: Utilization sprintf Formatting string 
  $format_num = sprintf("%.2f",$num);
  echo $format_num; //10.46
   
  // No. 1 3 Type: A function that uses thousands of digits to format numbers number_format()
  echo number_format($num, 2); //10.46
  // Or as follows 
  echo number_format($num, 2, '.', ''); //10/46

ps: PHP digits (price) are reserved to two decimal places

Let's look at the price calculation or price display of some commodities in PHP, which needs to be accurate to two digits after the decimal point, which is the unit of points in our usual RMB. How to show the price of goods in PHP and keep it in sub-units? The following tutorial will explain 1.

php commodity price, php keep two decimal places, php commodity price display

PHP number_format () Function

number_format():函数可以通过千位分组的形式来格式化数字。

Syntax:

number_format(number,decimals,decimalpoint,separator)

Parameters:

number: Required. The number to format.
decimals: Optional. Specify how many decimals.
decimalpoint: Optional. Specifies the string used as the decimal point.
separator: Optional. Specifies the string used as a thousand separator.

Example: PHP commodity price is in yuan, with two decimal places reserved

Code:


<?php
$a = 10;
echo number_format($a,'2');
$b = 1000000;
echo number_format($b,'2');
$c = 5458.5684;
echo number_format($c,'2');
$d = '1254.8963';
echo number_format($d,'2');
$e = '88.9643';
echo number_format($e,'2');
?>

Output:

10.00
1,000,000.00
5,458.57
1,254.90
88.96

The above example summarizes:

1. Numbers of either number type or string type can be manipulated by the number_format () function
2. When number_format () operates on numbers that do not contain decimals, if the number of decimals is set, it will be supplemented in the form of 0.
3. If there are many decimal numbers in the operation, the nearest 1 digit will be selected in the way of 4 rounding and 5 entering
4. If the 3rd and 4th parameters of number_format () are not set, if the integer part is larger than 3 digits, then every 3 digits are divided by a ',' sign from the left to the right of the decimal point

Example PHP number_format () Remove the dividing symbol of the integer part

Code:


<?php
echo number_format("1000000",2,".","");
echo number_format("1000000",2,".","x");
echo number_format("1000000",2,"y","x");
?>

Output:

1000000.00
1x000x000.00
1x000x000y00

The above example summarizes:

1. The third parameter of number_format () function can replace the display mode of small numbers, such as replacing the decimal point with y
2. The fourth parameter of number_format () function can replace the thousandth dividing symbol of integer part, such as null or X
3. Note that the third parameter and the fourth parameter of number_format () function coexist, so you can't fill in only one parameter.


Related articles: