Home  >  Article  >  Backend Development  >  How to round the value after division in php

How to round the value after division in php

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-02 11:37:22918browse

php method to implement rounding of values ​​after division: 1. Create a php sample file; 2. Create two variables, namely "$myval" and "$const"; 3. "round()" The function sets the value after division, and the syntax is "$result = round($myval / $const, 2, PHP_ROUND_HALF_UP);"; 4. This can be achieved by outputting the "$result" assignment result through echo.

How to round the value after division in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

The operation of rounding the value after division in PHP can be completed using the built-in "round()" function.

The syntax is "round(float $val, int $precision = 0, int $mode = PHP_ROUND_HALF_UP)"

  • $val needs to be processed Floating point number;

  • #$precision is the number of decimal places retained, the default is 0

  • $mode is the mode that needs to be used for rounding

Code examples are as follows:

1. Create a php sample file

<?php
////php代码
?>

2. Create two variables

$myval = 123.456;
$const = 7.89;

3 . Use the "round()" function to set the mode of taking values ​​after division, and echo will output the result.

// 除以常量值后,保留两位小数并向上(四舍五入)
$myval = 123.456;
$const = 7.89;
$result = round($myval / $const, 2, PHP_ROUND_HALF_UP);
echo $result; // 输出 15.63

The above is the detailed content of How to round the value after division 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