Summary of data rounding methods in PHP [4 methods]

Time:2020-5-16

In this paper, the method of data rounding in PHP is summarized. To share with you for your reference, as follows:

In PHP development, sometimes we will encounter the data rounding operation. This paper shares four methods of data rounding with PHP.

PHP implements four methods of data rounding, respectively throughnumber_format()Functionround()Functions andsprintf()Formatting andintval()Function output method to achieve four rounding five. Strictly speaking, the last one is not a strict rounding. The last one is only to remove the decimal part of the data and only keep the integer part. The details will be discussed below.

1. The number ou format() method implements rounding

number_format()Function to format numbers by grouping them in thousands.

<?php
//Define a variable of type float
$number = 1234.5678;
//English Notation (defult)
$number_format_english = number_format($number);
//1,235
$number_format_english = number_format($number, 2, '.', '');
//1234.57
//French Notation
$number_format_francais = number_format($number, 2, ',', '');
//1234,57
$number_format_francais = number_format($number, 3, ',', '');
//1234,568
echo $number_format_english;
//1234.57
echo $number_format_francais;
//1234,568

2. Round() method to round

round()Function to round floating-point numbers.

<?php
//Define a variable of type float
$number = 1234.5678;
//No decimal
echo round($number);
//1235
//Keep two little trees
echo round($number,2);
//1234.57
echo "<br>";
$number = 12345678;
//Rounding in thousandths
echo round($number,-4);
//12350000

3. Sprintf () format input to achieve rounding

The main function of the string format command is to write the formatted data into a string.sprintfIt’s a function of arguments.

<?php
//Define a positive integer
$n = 43951789;
//Define a negative integer
$u = -43951789;
// ASCII 65 is 'A'
$c = 65; 
printf("%%b = '%b'\n", $n); 
//%b = '10100111101010011010101101'
printf("%%c = '%c'\n", $c); 
//%c = 'A' 
printf("%%d = '%d'\n", $n); 
//%d = '43951789' 
printf("%%e = '%e'\n", $n); 
//%e = '4.395179e+7' 
printf("%%u = '%u'\n", $n); 
//%u = '43951789' 
printf("%%u = '%u'\n", $u); 
//%u = '4251015507' 
printf("%%f = '%f'\n", $n); 
//%f = '43951789.000000' 
printf("%%o = '%o'\n", $n); 
//%o = '247523255' 
printf("%%s = '%s'\n", $n); 
//%s = '43951789' 
printf("%%x = '%x'\n", $n); 
//%x = '29ea6ad' 
printf("%%X = '%X'\n", $n); 
//%X = '29EA6AD' 
printf("%%+d = '%+d'\n", $n); 
//%+d = '+43951789' 
printf("%%+d = '%+d'\n", $u); 
//%+d = '-43951789'

4. Intval() function for integer output

This method is not strictly rounding, which forces the decimal part of the data to be cleared to the effect of outputting only the integer part.

<?php
//Define a floating point number
$number = 1234.5678;
$number_int = intval($number);
echo $number_int;
//1234

PS: Here we recommend several computing tools for your further reference:

Calculation tools for online univariate function (equation) solution:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

On line use of scientific calculator_ Advanced calculator online computing:
http://tools.jb51.net/jisuanqi/jsqkexue

Online calculator? Standard calculator:
http://tools.jb51.net/jisuanqi/jsq

For more information about PHP, please refer to the following topics: summary of PHP mathematical operation skills, summary of PHP operation and operator usage, summary of PHP string usage, complete operation skills of PHP array, PHP data structure and algorithm tutorial, summary of PHP programming algorithm and summary of PHP regular expression usage

I hope that this article will be helpful for PHP programming.