我正在尝试使用 redis 精确创建记分牌。但它是四舍五入的精度。
例如
127.0.0.1:6379> zadd leaderboard 30.1441050792000 "item"
(integer) 1
127.0.0.1:6379> zrevrange leaderboard 0 -1 withscores
1) "item"
2) "30.144105079199999"
有什么方法可以保持redis中的精度来存储准确的结果。
谢谢
最佳答案
根据文档
Redis sorted sets use a double 64-bit floating point number to represent the score. In all the architectures we support, this is represented as an IEEE 754 floating point number
因此,如果您需要那么高的精度,您可以存储 0 - 2^53 范围内的整数。然后,您可以在读取数据时除以乘数。
例子:
127.0.0.1:6379> zadd leaderboard 301441050792000 item
(integer) 1
127.0.0.1:6379> zrevrange leaderboard 0 -1 WITHSCORES
1) "item"
2) "301441050792000"
然后除以 1e13。
注意
这将使用的最大值是 900.7199254740992。之后超过 53 位并以丢失的精度结束。
关于Redis 不保留排序集中的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384297/