Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove decimal places from php

Shulou Source: shulou.com Published: 2022-06-03 13:50:31 10月02日 Update

This article mainly introduces php how to remove decimal places, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Php to remove decimal places method: 1, use intval () function to get the integer part of the variable, syntax "intval ($number)"; 2, use floor () function to round down, syntax "floor ($number)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of removing decimal places in php

Method 1: use the intval () function

The intval () function is used to get the integer value of a variable. Syntax format:

Int intval (mixed $var [, int $base = 10])

$var: the numerical value to be converted to integer.

$base: the base used for conversion.

If base is 0, determine which binary to use by examining the format of the var:

Use hexadecimal (hex) if the string includes the prefix of "0x" (or "0X"); otherwise

Use octal (octal) if the string starts with "0"; otherwise

Decimal (decimal) will be used.

The intval () function is often used for data type conversion, converting variables of string and floating point types to integer types.

Example: convert a decimal to an integer (remove decimal places)

Output result:

Method 2: use the floor () function to round down

The floor () function rounds down to the nearest integer. Syntax format:

Floor ($number)

$number: required. Specifies the value that needs to be rounded down.

Example: rounding decimal places (removing decimal places)

Output result:

Add: two other methods of decimal rounding

Use the ceil () function-- rounding up, adding 1 to the integer part if there is a decimal.

Output result:

Use the round () function-- rounded

Output result:

Thank you for reading this article carefully. I hope the article "how to remove decimal places in php" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Decimals functions decimal places variables types methods decimal numeric integer article results syntax output characters strings formats examples parts rounding two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology MySQL Shulou Tech Info Linux