Home > Backend Development > PHP Problem > How to use round in php

How to use round in php

藏色散人
Release: 2023-03-09 21:02:01
Original
3222 people have browsed it

The function of the round function is to round floating point numbers. The usage in PHP is "round(x,prec)", where the parameter x specifies the number to be rounded, and the parameter prec specifies the number of digits after the decimal point. .

How to use round in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

The round() function rounds floating point numbers.

Syntax

round(x,prec)
Copy after login

Parameters

x Optional. Specifies the number to be rounded.

prec Optional. Specifies the number of digits after the decimal point.

Description

Returns the result of rounding x according to the specified precision prec (the number of decimal digits after the decimal point). prec can also be negative or zero (default).

Tips and Notes

Note: PHP cannot handle strings like "12,300.2" correctly by default.

Note: The prec parameter was introduced in PHP 4. .

Example

<?php
echo(round(0.60));
echo(round(0.50));
echo(round(0.49));
echo(round(-4.40));
echo(round(-4.60));
?>
Copy after login

Output:

1
1
0
-4
-5
Copy after login

Recommended learning: "PHP Video Tutorial"

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

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