Home > Backend Development > PHP Tutorial > How to remove two digits after decimal point in php

How to remove two digits after decimal point in php

PHPz
Release: 2023-04-11 13:08:01
Original
2093 people have browsed it

PHP is an open source language that is widely used in the field of web development. In web development, it is often necessary to process and format data, especially numbers, such as removing two digits after the decimal point. How to implement this function in PHP? This article will introduce it to you in detail.

1. Use the number_format() function

The number_format() function is a function in PHP specifically used to format numbers. This function formats a floating point number into a string with thousands separator and decimal point. We can use this function to get the value with two decimal places removed.

The specific implementation method is as follows:

$num = 123.456; // 原始数值
$format_num = number_format($num, 2, '.', ''); // 格式化后的数值,去掉小数点后两位
echo $format_num; // 输出:123.45
Copy after login

number_format() function has three required parameters and one optional parameter. The parameter description is as follows:

  • The first parameter is the value to be formatted.
  • The second parameter is the number of digits retained after the decimal point. It needs to be set to 2, that is, two decimal places are retained.
  • The third parameter is the decimal separator, which is set to '.' here.
  • The fourth parameter is the thousands separator, which is not set here.

Use this function to easily remove the two digits after the decimal point.

2. Use the round() function

The round() function is a function used for rounding in PHP. You can use this function to remove two decimal places from a value.

The specific implementation is as follows:

$num = 123.456; // 原始数值
$format_num = round($num, 2); // 去掉小数点后两位,同时进行四舍五入
echo $format_num; // 输出:123.46
Copy after login

The round() function has two required parameters. The first parameter is the value to be processed, and the second parameter is the number of digits to be retained after the decimal point. . Use this function to retain values ​​to two decimal places and round them.

3. Use the sprintf() function

The sprintf() function is a function in PHP that formats strings. This function can output a formatted string according to the specified format.

The specific implementation method is as follows:

$num = 123.456; // 原始数值
$format_str = sprintf("%.2f", $num); // 格式化后的字符串,去掉小数点后两位
echo $format_str; // 输出:123.46
Copy after login

The sprintf() function has two required parameters. The first parameter is the format string to be output, here it is set to "%.2f" , where %f indicates that the value to be output is a floating point number, and .2 indicates that two decimal places are retained. The second parameter is the value to be formatted, which is $num.

Summary:

The above introduces three methods of removing two digits after the decimal point. The number_format() function can format floating point numbers into strings with decimal points and thousands separators, while the round() function directly rounds the values, and the sprintf() function can format the values ​​into strings in a specified format. Each of these methods has its own advantages and disadvantages, and you can choose according to actual needs.

The above is the detailed content of How to remove two digits after decimal point 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