How do I get a float value when dividing two integ

2019-02-16 08:42发布

问题:

Hi I am trying to divide two integers ex: 12/13 but I always get a whole integer 1 not a decimal number.

I tried type casting the values to float before hand with no success.

Basically all I want is a decimal result like: 0.923...

$x = 12;
$y = 13;
echo $value = $x / $y; //Would like to see 0.923 not 1

回答1:

Under normal circumstances your code should return the floating value 0.923076...

The reason you get a rounded integer might be because you have your ini setting for "precision" set to 0, to fix this either edit your php.ini or use ini_set("precision", 3); in your code before the calculation.

Another way to workaround this is to use BCmath:

echo $value=bcdiv($a, $b, 3);

And yet another way without using any extension is to use a little math trick by multiplying the value you want to divide by 1000 to get 3 decimals.
This way you'll divide 12000 by 13 and the whole part will be 923, then since you multiplied by 1e3 insert a comma/dot before the last most 3 places.

function divideFloat($a, $b, $precision=3) {
    $a*=pow(10, $precision);
    $result=(int)($a / $b);
    if (strlen($result)==$precision) return '0.' . $result;
    else return preg_replace('/(\d{' . $precision . '})$/', '.\1', $result);
}

echo divideFloat($a, $b); // 0.923



回答2:

echo $value = $x / (float) $y;

if you cast the variable $y as float the interpreter use the floating point division instead of integer division.

because it's default asumption to use integer division on two integer variables.

It was different if you used $y = 13.0 (a float variable as denominator): The results is always a float number



回答3:

All other answers are NOT RIGHT, because PHP's division will always return float values, as is stated clearly in official manual PHP: Arithmetic Operators , except for cases when two operands are both integers which can be evenly divided.

In fact, the question is wrong: the code should produce 0.923..., as was expected from the questioner.

Sarcastically, the voted-down answer (came from @BulletProof47) is the only other one which is just NOT WRONG (but also meaningless). Who knows what he was thinking, but I bet everybody knows why it was voted down :D

In case who is interested, the underlying function which does division in php is div_function, located in Zend/zend_operators.c, shown below:

ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
{
    zval op1_copy, op2_copy;
    int converted = 0;

    while (1) {
        switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
            case TYPE_PAIR(IS_LONG, IS_LONG):
                if (Z_LVAL_P(op2) == 0) {
                    zend_error(E_WARNING, "Division by zero");
                    ZVAL_BOOL(result, 0);
                    return FAILURE;         /* division by zero */
                } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == LONG_MIN) {
                    /* Prevent overflow error/crash */
                    ZVAL_DOUBLE(result, (double) LONG_MIN / -1);
                    return SUCCESS;
                }
                if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
                    ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
                } else {
                    ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
                }
                return SUCCESS;
    ...


回答4:

Just use $value = (float)($x/$y); //Result will in float.

Cheers!