如何在go中比较两个Mongodb原始类型Decimal128

valueA, _ := primitive.ParseDecimal128("123.00")
valueB, _ := primitive.ParseDecimal128("123.12")

如何计算 valueA < valueB?


动漫人物
浏览 155回答 2
2回答

大话西游666

我找到了答案有一个函数将 Decimal128 转换为 BigInt。和 BigInt 可供比较func CompareDecimal128(d1, d2 primitive.Decimal128) (int, error) {&nbsp; &nbsp; b1, exp1, err := d1.BigInt()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; }&nbsp; &nbsp; b2, exp2, err := d2.BigInt()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return 0, err&nbsp; &nbsp; }&nbsp; &nbsp; sign := b1.Sign()&nbsp; &nbsp; if sign != b2.Sign() {&nbsp; &nbsp; &nbsp; &nbsp; if b1.Sign() > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1, nil&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1, nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if exp1 == exp2 {&nbsp; &nbsp; &nbsp; &nbsp; return b1.Cmp(b2), nil&nbsp; &nbsp; }&nbsp; &nbsp; if sign < 0 {&nbsp; &nbsp; &nbsp; &nbsp; if exp1 < exp2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1, nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return -1, nil&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if exp1 < exp2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1, nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return 1, nil&nbsp; &nbsp; }}*为指数部分编辑

沧海一幻觉

在这里查看测试:func compareDecimal128(d1, d2 primitive.Decimal128) bool {&nbsp; &nbsp; d1H, d1L := d1.GetBytes()&nbsp; &nbsp; d2H, d2L := d2.GetBytes()&nbsp; &nbsp; if d1H != d2H {&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }&nbsp; &nbsp; if d1L != d2L {&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }&nbsp; &nbsp; return true}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go