Home  >  Article  >  Backend Development  >  How to display only 3 decimal places in php

How to display only 3 decimal places in php

WBOY
WBOYOriginal
2021-11-17 10:29:063507browse

In PHP, you can use the number_format() function to display 3 decimal places. This function can format numbers by thousands grouping. You can set the number of decimal places in the returned result. The syntax is "number_format(to format digital objects,3);".

How to display only 3 decimal places in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php only displays 3 How to use decimal places

1. Create a new php file named test.php to explain how to set the number of decimal places in php. Use the header() method to set the encoding format of the page to utf-8. Create a decimal variable for testing.

How to display only 3 decimal places in php

2. In the test.php file, use the number_format function to keep the number with 3 decimal places, and the result is saved in the $res variable. Output the result after decimal processing through echo.

How to display only 3 decimal places in php

Open the test.php file in the browser to view the results.

How to display only 3 decimal places in php

Summary:

1. Create a decimal variable for testing.

2. Use the number_format function to keep the number with 3 decimal places, and the result is saved in the $res variable.

3. Output the result after decimal processing through echo.

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to display only 3 decimal places 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