简体   繁体   English

在PHP中需要一些有关数字的帮助

[英]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: 我在PHP中的数字有一点问题,这基本上就是我想做的事情:

$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). $uni是有问题的数字,也是我需要使用的数字(数字多少并不重要,但可以是成千上万,也可以是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. 我正在尝试计算$uni可以输入多少次“ 100”,这并不是真正的问题,问题是我需要将剩余的数字(小数点后)正确格式化。

For example: 例如:

(Just working with numbers over 100) (仅处理超过100的数字)

If I have $uni as "356" then I need to output "3 Credits" ( $credearned = 3; ) & "56" ( $per = 56; ) left over. 如果我将$uni作为“ 356”,则需要输出$credearned = 3; “ 3学分”( $credearned = 3; )&“ 56”( $per = 56; )。

Also, I need to strip certain numbers so that if $per is "05" it has to be just "5". 另外,我需要去除某些数字,以便如果$per为“ 05”,则它必须仅为“ 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 ; 这也适用于像05这样的数字; you'll get 0 for $credits and 5 for $left_over . $credits可获得0, $left_over 5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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