Home  >  Article  >  Backend Development  >  Method and example analysis of using number_format function to intercept decimals in PHP

Method and example analysis of using number_format function to intercept decimals in PHP

墨辰丷
墨辰丷Original
2018-06-02 09:52:222197browse

This article mainly introduces how PHP uses the number_format function to intercept decimals, and analyzes the related techniques of the number_format function for floating-point mathematical operations in the form of examples. Friends in need can refer to it

Everyone knows how to use PHP The number_format() function can group numbers into thousands. But it will round the numbers. Is there a way to make it directly discard the decimals after the reserved digits without rounding? Don't worry, everyone, listen to me in detail, If you want to keep two decimal places, you can change your number to -0.005

For example:

123456.6588

The result you want is:

123,456.65

You can do this:

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

If you just want to keep integers, you can

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

Problem solved!

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpMethod to implement forced download file function

phpMethod to download various files based on header function

PHP method to create compressed image

The above is the detailed content of Method and example analysis of using 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