当JS遇到uint64:JS整数溢出问题

最近遇到一个问题,就是传了一个超级大的uint64,结果前端发现数字对不上,精度丢失了!原因就在于,JS的Number是 “64-bit floating point IEEE 754 number”。最大能表示的数是 Number.MAX_SAFE_INTEGER,一般来说,就是:

  • 2 ** 53 - 1, 或者
  • +/- 9,007,199,254,740,991, 或者
  • nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one

总之,就是如果表示整数,最多能表示到 2 ** 53 - 1。解决方案,就是用string来表示,对于GIN来说,就是在定义的struct 中,uint64的tag里,加一个 string,比如如果原来是 json:"id",现在就是 json:"id,string"

package main

import (
	"log"

	"github.com/gin-gonic/gin"
)

type Demo struct {
	ID uint64 `json:"id,string"`
}

func bigNumHandler(c *gin.Context) {
	d := Demo{}
	if err := c.BindJSON(&d); err != nil {
		log.Printf("error: %s", err.Error())
		c.JSON(400, d)
		return
	}

	d.ID -= 2
	c.JSON(200, d)
}

func main() {
	route := gin.Default()
	route.POST("/big", bigNumHandler)
	route.Run(":8085")
}
$ http :8085/big id=18446744073709551615
HTTP/1.1 200 OK
Content-Length: 29
Content-Type: application/json; charset=utf-8
Date: Tue, 22 Jun 2021 02:51:10 GMT

{
    "id": "18446744073709551613"
}


$ http :8085/big id='18446744073709551615'
HTTP/1.1 200 OK
Content-Length: 29
Content-Type: application/json; charset=utf-8
Date: Tue, 22 Jun 2021 02:51:13 GMT

{
    "id": "18446744073709551613"
}

这个坑,比较难遇上,你得同时遇到:

  • 刚好你用了 uint64/int64
  • 刚好你的值大于 2 ** 53

以上就是这个坑的总结。



更多文章
  • jQuery简明教程
  • Python RQ(Redis Queue)添加gevent支持
  • 读《超级运营术》- 如何做社区?
  • 技术人,光有技术是不行的
  • 使用shairport-sync搭建airplay音频服务器
  • 搭建aria2服务器
  • VirtManager Windows自适应屏幕
  • 使用btrfs组建RAID1
  • Swagger? 不好用
  • Golang/Python最佳实践
  • 读《毛泽东选集》
  • GORM源码阅读与分析
  • 随想
  • Golang中的错误处理
  • Golang 的槽点