简体   繁体   中英

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; ) left 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 .

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