Home >Backend Development >PHP Problem >How to remove decimal places in php

How to remove decimal places in php

青灯夜游
青灯夜游Original
2021-09-15 12:22:434241browse

php method to remove decimal places: 1. Use the intval() function to get the integer value of the variable, the syntax "intval($number)"; 2. Use the floor() function to round down, the syntax " floor($number)”.

How to remove decimal places in php

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

php removes decimal places Method

Method 1: Use the intval() function

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

int intval ( mixed $var [, int $base = 10 ] )
  • $var: The quantity value to be converted to integer.

  • #$base: The base used for conversion.

    If base is 0, determine the base used by detecting the format of var:

    • If the string includes "0x" (or "0X") Prefix, use hexadecimal (hex); otherwise,

    • If the string starts with "0", use octal (octal); otherwise,

    • Decimal will be used.

The intval() function is often used to convert data types, converting string and floating-point type variables into integer types.

Example:Convert decimal to integer (remove decimal places)

<?php
header("Content-type:text/html;charset=utf-8"); 
$num = 123.456;
$int = intval($num);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
var_dump($int);
?>

Output result:

How to remove decimal places in php

Method 2: Use the floor() function--round down

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

floor($number);
  • $number: required. Specifies the value to be rounded down.

Example:Round decimals (remove decimal places)

<?php
header("Content-type:text/html;charset=utf-8"); 
$num = 123.456;
$int = floor($num);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
var_dump($int);
?>

Output result:

How to remove decimal places in php

Supplement: Two other methods of decimal rounding

  • Use the ceil() function--round up, if there is a decimal, add 1 to the integer part

<?php
header("Content-type:text/html;charset=utf-8"); 
$num = 123.456;
$int = ceil($num);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
var_dump($int);
?>

Output result:

How to remove decimal places in php

  • ##Use round() function--rounding

  • <?php
    header("Content-type:text/html;charset=utf-8"); 
    $num = 123.456;
    $int = round($num);
    echo &#39;小数值为:&#39;.$num.&#39;<br>&#39;;
    echo &#39;取整后值为:&#39;.$int .&#39;<br>&#39;;
    var_dump($int);
    
    $num = 123.556;
    $int = round($num);
    echo &#39;小数值为:&#39;.$num.&#39;<br>&#39;;
    echo &#39;取整后值为:&#39;.$int .&#39;<br>&#39;;
    var_dump($int);
    ?>
Output results:

How to remove decimal places in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove decimal places 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