Home  >  Article  >  Backend Development  >  How to handle thousands of numbers with commas in php_PHP Tutorial

How to handle thousands of numbers with commas in php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:04880browse

Number_format(); is usually used to format numbers. By default, thousands characters are separated by commas, for example:

Copy code The code is as follows:

echo number_format("10000.01231", 2);
//Take 2 digits after the decimal point, the output result is: 10,000.01 Thousand digits are separated by commas by default.

It would be a bit difficult if we verify the numbers in this format obtained from the client in the background.

Old repairmen usually use filter_input(INPUT_POST,"price",FILTER_VALIDATE_FLOAT) to verify the price. If it is a thousand characters with commas, it will not be obtained.

So when confirming that the input is a number, you must first filter out all thousands characters and then verify.

In fact, few people enter numbers with thousands characters, unless they copy or resubmit the original output value.

When outputting, we can use as little number formats with thousand characters as possible. The output can be written like this:
Copy code The code is as follows:

format_number("10000.01231",2,".","");
//The output is 10000.01

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324757.htmlTechArticleUsually use number_format(); to format numbers. By default, thousands of characters are separated by commas, such as: Copy the code as follows: echo number_format("10000.01231", 2); //After the decimal point...
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