使用 PHP,我如何制作一个计数器,将任何 Int 转换为 1 到 9999?
我有一个内部计数器,从 0-999999999999 计数。
我希望它显示为 0-9999 之间的数字,然后再次翻转。
这意味着:
- 0 显示为 1
- 1 显示为 2
- 9998 显示为 9999
- ...
- 9999 显示为 1
- 10000 显示为 2
- ...
- 19999 显示为 1
- 20000 显示为 2
编辑:
1 + $number % 9999 是答案(谢谢@布拉德·克里斯蒂)。我的预期结果表是错误的。 (感谢@Tevo D)
I have an internal counter that counts from 0-999999999999.
I would like this to display as a number between 0-9999, then rollover again.
This means:
- 0 displays as 1
- 1 displays as 2
- 9998 displays as 9999
- ...
- 9999 displays as 1
- 10000 displays as 2
- ...
- 19999 displays as 1
- 20000 displays as 2
edit:
1 + $number % 9999 was the answer (Thanks @Brad Christie). My table of expected results is wrong. (Thanks @Tevo D)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用MOD,它会给你超过9999的余数(例如任何数字除以9999都可以进行N次,余数为Y(你最终会得到Y作为一个值)
对于你的数字查找时,您可能需要对 MOD (
%
) 之后获得的任何值进行+1
操作,或使用10000
另请参阅模数运算符
Use MOD, it will give you the remainder past 9999 (e.g. Any number divided by 9999 can go in N times, with a remainder of Y (you'll end up with Y as a value)
For the numbers you're looking for, you may want to
+1
any value you get after the MOD (%
), or use10000
See also the Modulus Operator
以 10000 进行提醒,保证结果在 0 到 9999 之间并滚动
Take reminder with 10000, that guarantees the result to be in between 0 and 9999 and rolls over
您的价值观表与您的要求不符。在示例中,您使用 9999 个结果值 (1-9999) 来表示 10000 个输入值。在文本中你说的是 10000 个输出值 (0-9999)。
我认为这就是您真正要求的。该算法将输出 1-9999,然后再次翻转到 1。
换句话说,此解决方案将提供一个四位非零值:
输出将与您的示例不匹配,因为您的示例每 10000 个值滚动一次,而不是 9999 个值。以下是输出:
Your table of values doesn't match what you asking for. In the example you are using 9999 result values (1-9999) for 10000 input values. In the text you are saying 10000 output values (0-9999).
Here is what I think you are really asking for. This algorithm will output 1-9999 and then roll over to 1 again.
In other words, this solution will provide a four digit non-zero value:
The output will NOT match your example, as your example has it rolling over every 10000 values, not 9999. Here is the output: