Home > Backend Development > PHP Problem > How to convert to two decimal places in php

How to convert to two decimal places in php

藏色散人
Release: 2023-03-06 18:38:02
Original
2733 people have browsed it

How to convert PHP to two decimal places: 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.

How to convert to two decimal places in php

Recommended: "PHP Video Tutorial"

Three ways to retain two decimal places in PHP

/**
 * 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
Copy after login

The above is the detailed content of How to convert to two decimal places in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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