简体   繁体   中英

php calculate percentage with decimal answer

I don't understand what I am doing wrong. I've got a number, eg 220. Then I need to increase it with for example 11%, so 220 * 11% = 244.2, but my answer is 2420?

I've tried the following:

        echo '<br>';
        echo $col0 . '<br>'; //outputs 220

        settype($col0New, "decimal");
        $col0New = ($col0 * '11%') + $col0;

        echo $col0New    . '<br>'; //outputs 2640 but should be 244.2?

        $col0New1 = number_format($col0New,2);
        echo $col0New1   . '<br>'; //outputs 2,640.00

Please help.

There's no need for making it any more complex than you need. Basic math allows you to just multiply with a constant, more specific, if you multiply by 1.11 , you'll get an increase of 11%.

You can simply do it like this

echo $col0 * 1.11; // Outputs 244.20

You can't use the % as percentage in php . To Accompish what you want, you need to rewrite your percentage to it's decimal form. Ex: 55% will be 0.55

You need to stay with pure numbers: 240 * 1.11. Your '11%' is a string literal, which PHP must fist convert to the number 11, and then multiplies by 240, which explains where 2420 comes from.

.11 is 11%, but you want to add 1 to increase the value to get 111%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM