How to use number_format function to intercept decimals in PHP

怪我咯
Release: 2023-03-14 07:38:02
Original
1966 people have browsed it

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, ".", ",");
Copy after login

If you only want to keep integers you can

$num=123456.6588;
number_format($num-0.5);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template