When processing data of a numeric type, conversion can be done in various ways when it is necessary to convert the number of decimal places. The most commonly used methods are rounding, rounding, and discarding.
floor (value to convert) // decimal point discarded
ceil (value to convert) // Decimal point
round (value to convert) // round to decimal
You can use it simply as above. Then look at the example below.
* See an example of decimal handling in php
If the variable $ testnum exists as below, it can be converted as follows.
<? php
$testnum = 12.345;
echo floor ($testnum);
// Outputs 12. Decimal point discarded
echo ceil ($testnum)
// Outputs 13. Decimal Point Rounded
echo round ($testnum)
// Output 12. Rounding applied to output
?>
* How to output only the desired number of decimal places
You can use the round () function to return only the desired number of rounds. Look at the example below.
<? php
$testnum = 12.3456;
echo round ($testnum, 2);
// Outputs 12.35. Round off to two decimal places
echo round ($testnun, 3);
// output 12.345
?>
'개발 꿀팁 > PHP' 카테고리의 다른 글
php에서 \r\n과 \n의 치환되지 않는 오류 해결 (0) | 2022.06.24 |
---|---|
[PHP] Check for the existence of a variable, Isset() Empty() (0) | 2018.01.19 |
[PHP function] URL page image tag extraction (0) | 2018.01.16 |
Remove empty elements from PHP array (0) | 2018.01.16 |
[PHP] Back to previous page (0) | 2018.01.16 |