php 除以整数并得到整数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12832557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:19:00  来源:igfitidea点击:

Divide integer and get integer value

php

提问by Ismail Saleh

In languages like C or Python, if I divide an integer by an integer, I get an integer:

在像 C 或 Python 这样的语言中,如果我将一个整数除以一个整数,我会得到一个整数:

>>> 8/3
2

But in PHP, if I divide an integer by another integer with /, sometimes I get a float:

但是在 PHP 中,如果我用 将一个整数除以另一个整数/,有时我会得到一个浮点数:

php > var_dump(6/3);
int(2)
php > var_dump(8/3);
float(2.6666666666667)

I'd like to do division like in Python or C, so that 8/3 is 2. How can I do that in PHP?

我想在 Python 或 C 中进行除法,所以 8/3 是 2。我如何在 PHP 中做到这一点?

回答by iLaYa ツ

use round()function to get integer rounded value.

使用round()函数获取整数舍入值。

round(8 / 3); // 3

or

或者

Use floor()function to get integer value

使用floor()函数获取整数值

floor(8 / 3); // 2

回答by Marek Lisy

In PHP 7, there is intdivfunction doing exactly what you want.

在 PHP 7 中,有一个intdiv函数完全符合您的要求。

Usage:

用法:

intdiv(8, 3);

Returns 2.

返回2

回答by S.P.

There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it downwards, or the round() function provides finer control over rounding.

PHP 中没有整数除法运算符。1/2 产生浮点数 0.5。该值可以强制转换为整数以将其向下舍入,或者 round() 函数提供对舍入的更好控制。


var_dump(25/7);           // float(3.5714285714286)    
var_dump((int) (25/7));   // int(3)   
var_dump(round(25/7));    // float(4)     

PhP manual

博士手册

回答by bipen

use this....

用这个....

intval(1700000 / 300000 )...

this returns the integer value.

这将返回整数值。

回答by mahipal purohit

(int)(1700000 / 300000);

use type casting.

使用类型转换。

回答by Besi

You can use the shortform by adding |0 to the end

您可以通过在末尾添加 |0 来使用缩写

8/3|0

8/3|0

回答by Colin D Bennett

There are several ways to perform integer division in PHP. The language doesn't have an operator for integer division, but there are several options for rounding the floating point quotient to an integer:

在 PHP 中有几种执行整数除法的方法。该语言没有用于整数除法的运算符,但有几种将浮点商四舍五入为整数的选项:

<?php
$pos = 1;
$neg = -1;
$divisor = 2;

// No rounding (float division)
var_dump($pos / $divisor);          //  0.5 (float)
var_dump($neg / $divisor);          // -0.5 (float)

// Round toward zero (like C integer division)
var_dump((int)($pos / $divisor));   //  0 (int)
var_dump((int)($neg / $divisor));   //  0 (int)

// Round half away from zero
var_dump(round($pos / $divisor));   //  1 (float)
var_dump(round($neg / $divisor));   // -1 (float)

// Round down
var_dump(floor($pos / $divisor));   //  0 (float)
var_dump(floor($neg / $divisor));   // -1 (float)

# And on PHP 7 you can round toward zero with intdiv():
var_dump(intdiv($pos, $divisor));   //  0 (int)
var_dump(intdiv($neg, $divisor));   //  0 (int)  Rounded toward zero

On PHP 7 you can use intdiv($p, $q)to directly perform integer division. This is equivalent to (int)($p / $q)on PHP 5.

在 PHP 7 上,您可以使用它intdiv($p, $q)来直接执行整数除法。这相当于(int)($p / $q)在 PHP 5 上。

回答by Aamir Kalimi

FOR PHP 7 try intdiv()Function:

对于 PHP 7 尝试intdiv()功能:

Syntax:int intdiv($dividend, $divisor)

句法:int intdiv($dividend, $divisor)

          <?php           
           $dividend = 19; 
           $divisor = 3;  
           echo intdiv($dividend, $divisor); 
           ?>

For Older versions of PHP:

对于旧版本的 PHP:

         <?php
     // Convert $total_minutes to hours and minutes.

         $total_minutes = 640;
         $minutes = $total_minutes % 60;
         $hours = ($total_minutes - $minutes) / 60;

         echo "Time taken was $hours hours $minutes minutes";
         ?>

回答by sanjay.chouhan180

For PHP version 7 => intdiv(a,b)

对于 PHP 版本 7 => intdiv(a,b)

And for versions less than 7 (like 5.6) => (int)floor(abs(a/b))

对于小于 7 的版本(如 5.6)=> (int)floor(abs(a/b))