简体   繁体   中英

More elegant way of adding a percentage?

I'm wondering if there is a more elegant way of adding a percentage. For example, if I wanted to add 12% of $a 's value to $a I'm doing:

$a = 100;
$a = $a + (($a / 100) * 12);

Which gives me 112 as the new value, of course. But I can't help but feel this is a little too verbose and there must be a better way of doing the percentage addition.

I tried:

$a = $a + 12%;

But, as expected this doesn't work. Before I consider writing a function myself to make this easier, is there a pre-existing way to do this?

Why not $a *= 1.12; ?

"Percent" comes from latin "per centum", or "per hundred". One percent is 1/100. So twelve percent is 0.12 - twelve hundredths.

你需要做的就是:

$a *= 1.12;

This is how I do it:

$percent = 10;
$price *= (1 + $percent / 100);

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