开发者

Need some help with Numbers in PHP

I'm having a little problem with numbers in PHP, this is basically what I'm trying to do:

$uni is the number in question and the one I need to work with (it doesn't matter what the number is but it can be in the hundreds of thousands or just 5).

I'm trying to work out how many times "100" can go into into $uni, that's not really the problem, what is the problem is that I need the remainding number (after the decimal point) to be properly formatted.

For example:

(Just working with numbers over 100)

If I have $uni as "356" then I need to output "3 Credits" ($credearned = 3;) & "56" ($per = 56;) lef开发者_C百科t over.

Also, I need to strip certain numbers so that if $per is "05" it has to be just "5".

$uni = 190;
if($uni >101){
$credearned = $uni / 100;
$per = ;
}else{
$credearned = 0;
$per = $uni;
}

I'd really appreciate the help, and I hope my explanation wasn't too confusing.

Thanks.


This is what the % (modulus) operator is for, finding the remainder of one number divided by another:

echo 356 % 100; // 56

In your case, you can find the "credits" and "left over" in a couple of simple statements instead of a complex loop:

$uni = 190;
$credits = (int)($uni / 100); // 1
$left_over = $uni % 100;      // 90

This also works for numbers like 05; you'll get 0 for $credits and 5 for $left_over.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜