处理数字的最佳方法?获取和更改单个数字
我想获取一个 1 到 17 位数字,然后将其放入一系列逻辑循环中,从而更改该数字的各个数字。我需要能够执行以下操作:
1)快速了解数字中有多少位数字。有没有一个好的(有效的)php 方法来做到这一点?获取以 10 为底的对数是最好的方法吗?或者也许把它变成一个字符串并询问长度?
2)接下来,我需要知道每个数字是什么数字。我是否应该继续对数字执行 % 10 来解析每个数字(这样做也可以通过在循环中添加计数来帮助解决第一个问题)?
3)最后,我需要一个好方法来更改号码的各个数字。我应该使用正则表达式吗?或者我应该通过将不同的数字相加来建立一个新数字,每个数字代表一个数字(例如 1 + 30 + 400 + 2000 + 6000 = 62431)?我想我也可以用与上一个示例相同的方法调整原始数字,通过添加/减去原始数字来将每个数字调整为我想要的数字。
如果您觉得其他方法更快,请告诉我解决我的问题的其他方法。效率是关键。
I want to take a 1 to 17 digit number, then put it through a series of logic loops that will change various digits of the number. I need to be able to do the following:
1) A quick way to know how many digits there are in the number. Is there a good (efficient) php method to do this? Is getting log base 10 the best way? Or maybe turning it into a string and asking for the length?
2) Next, I need to know what number is in each digit. Should I just keep doing % 10 on the number to parse each digit (doing this would also help solve problem number 1 by adding a count in the loop)?
3) Finally, I need a good way to change the various digits of the number. Should I use regex? Or should I just build up a new number by adding various numbers together, with each number representing the digit (ex. 1 + 30 + 400 + 2000 + 6000 = 62431)? I guess I could also adjust the original number in the same method as the last example by adding/subtracting from it to adjust each digit to the one I want.
Please let me know about other ways to solve my problem if you feel they're faster. Efficiency is key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我决定写一个答案,因为目前接受的答案根本就是错误的。
double
数字的相对精度约为 1E-16。这不是 PHP 的限制,这是 IEEE 754double
的固有特性。因此,如果要操作双精度数的第 17 位,无论该数的大小是多少,使用对数和执行算术运算都是行不通的。您需要做的是将数字转换为字符串,操作字符串中的数字并从字符串中显示它,而不是将其转换回 double 。I decided to write an answer because the currently accepted one is simply wrong. The relative precision of
double
numbers is of the order of 1E-16. This is not a limitation of PHP, this is an intrinsic feature of IEEE 754double
. Hence, if you want to manipulate the 17-th digit of a double number, no matter what the magnitude of the number is, using logs and performing arithmetical operations will not do. What you need to do is to convert the number to the string, manipulate the digits in the string and display it from the string, without converting it back todouble
.您可以使用如下所示更改数字中的数字值:
对于您的情况,使用数字作为一次性数字更改的字符串是最有意义的。为了节省时间,如果您要更改多个数字,您应该将数字解析为数字数组。如果您需要帮助,请在评论中告诉我。
简单的基准测试示例:
使用
microtime(true)
获取以微秒为单位的浮点数时间戳(这就是 true 的用途)。获取该动作之前和之后的时间,并将它们相减以获得总时间。You can change the value of a digit in a number with something like the following:
For your situation, using the number as a string for a one time number change makes the most sense. To save time, if you will be changing multiple digits you should parse the number into an array of digits. Let me know in the comments if you would like help with doing this.
Simple Benchmarking example:
Use
microtime(true)
to get the timestamp with microseconds as a float (that's what the true is for). Get the time before and after the action and subtract them to get the total time.