php method to retain decimals: 1. Use round() to round floating point numbers; 2. Use sprintf to format strings; 3. Use the function number_format() to format numbers using thousand digit grouping to retain decimals .
The operating environment of this article: Windows 7 system, PHP 7.1 version, DELL G3 computer
How do you keep several decimal places in php?
Three ways to retain two decimal places in PHP
The code is as follows:
/** * PHP保留两位小数的几种方法 * @link http://www.phpddt.com */ $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_num = sprintf("%.2f",$num); echo $format_num; //10.46 //第三种:利用千位分组来格式化数字的函数number_format() echo number_format($num, 2); //10.46 //或者如下 echo number_format($num, 2, '.', ''); //10/46
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to keep several decimal places in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
