使用 PHP,我如何制作一个计数器,将任何 Int 转换为 1 到 9999?

发布于 2024-12-15 03:18:28 字数 357 浏览 3 评论 0原文

我有一个内部计数器,从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤还要旧人安 2024-12-22 03:18:28
$x = 9280293;
$baseNineNineNineNine = $x % 9999;

使用MOD,它会给你超过9999的余数(例如任何数字除以9999都可以进行N次,余数为Y(你最终会得到Y作为一个值)

对于你的数字查找时,您可能需要对 MOD (%) 之后获得的任何值进行 +1 操作,或使用 10000

另请参阅模数运算符

$x = 9280293;
$baseNineNineNineNine = $x % 9999;

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 use 10000

See also the Modulus Operator

桃酥萝莉 2024-12-22 03:18:28

以 10000 进行提醒,保证结果在 0 到 9999 之间并滚动

$result = $int % 10000;

Take reminder with 10000, that guarantees the result to be in between 0 and 9999 and rolls over

$result = $int % 10000;
迷荒 2024-12-22 03:18:28

您的价值观表与您的要求不符。在示例中,您使用 9999 个结果值 (1-9999) 来表示 10000 个输入值。在文本中你说的是 10000 个输出值 (0-9999)。

我认为这就是您真正要求的。该算法将输出 1-9999,然后再次翻转到 1。

换句话说,此解决方案将提供一个四位非零值:

$result = $int % 9999 + 1;

输出将与您的示例不匹配,因为您的示例每 10000 个值滚动一次,而不是 9999 个值。以下是输出:

input   output
0       1
1       2
....      ....
9997      9998
9998      9999
9999      1       <--- 9999 * 1
10000     2
....      ....
19996     9998
19997     9999
19998     1       <--- 9999 * 2
19999     2
....      ....
19995     9998
19996     9999
19997     1       <--- 9999 * 3
19998     2
....      ....

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:

$result = $int % 9999 + 1;

The output will NOT match your example, as your example has it rolling over every 10000 values, not 9999. Here is the output:

input   output
0       1
1       2
....      ....
9997      9998
9998      9999
9999      1       <--- 9999 * 1
10000     2
....      ....
19996     9998
19997     9999
19998     1       <--- 9999 * 2
19999     2
....      ....
19995     9998
19996     9999
19997     1       <--- 9999 * 3
19998     2
....      ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文