Home  >  Article  >  Backend Development  >  How to use number_format function to intercept decimals in PHP

How to use number_format function to intercept decimals in PHP

怪我咯
怪我咯Original
2017-07-16 14:33:211958browse

number_format() FunctionFormats a number by thousands grouping. It returns a formatted number.

Note: This function supports one, two or four parameters (not three).

This article mainly introduces the method of intercepting decimals in PHP using number_formatfunction, and analyzes the related skills of number_format function for floating-point mathematical operations in the form of examples. It needs Friends, please refer to

Everyone knows that you can use the number_format() function of php to group numbers by thousands. However, it will round the numbers. Is there any way to directly discard and retain them? What about decimal places after digits without rounding?

If you want to keep two decimal places, you can put your number -0.005

For example:

123456.6588

you want The result to be obtained is:

123,456.65

You can do this:

$num=123456.6588;
echo number_format($num-0.005,2, ".", ",");

If you only want to keep integers you can

$num=123456.6588;
number_format($num-0.5);

The above is the detailed content of How to use number_format function to intercept decimals in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn