本文整理汇总了Golang中github.com/pingcap/tidb/mysql.Decimal.Equals方法的典型用法代码示例。如果您正苦于以下问题:Golang Decimal.Equals方法的具体用法?Golang Decimal.Equals怎么用?Golang Decimal.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pingcap/tidb/mysql.Decimal
的用法示例。
在下文中一共展示了Decimal.Equals方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testFrac
func testFrac(c *C, v mysql.Decimal) {
b := EncodeDecimal([]byte{}, v)
_, d, err := DecodeDecimal(b)
c.Assert(err, IsNil)
c.Assert(v.Equals(d), IsTrue)
c.Assert(v.FracDigits(), Equals, d.FracDigits())
c.Assert(v.String(), Equals, d.String())
}
示例2: EncodeDecimal
// EncodeDecimal encodes a decimal d into a byte slice which can be sorted lexicographically later.
// EncodeDecimal guarantees that the encoded value is in ascending order for comparison.
// Decimal encoding:
// EncodeInt -> value sign
// EncodeInt -> exp sign
// EncodeInt -> exp value
// EncodeBytes -> abs value bytes
func EncodeDecimal(b []byte, d mysql.Decimal) []byte {
if d.Equals(mysql.ZeroDecimal) {
return EncodeInt(b, zeroSign)
}
v := d.BigIntValue()
valSign := codecSign(int64(v.Sign()))
absVal := new(big.Int)
absVal.Abs(v)
value := []byte(absVal.String())
// Trim right side "0", like "12.34000" -> "12.34" or "0.1234000" -> "0.1234".
if d.Exponent() != 0 {
value = bytes.TrimRight(value, "0")
}
// Get exp and value, format is "value":"exp".
// like "12.34" -> "0.1234":"2".
// like "-0.01234" -> "-0.1234":"-1".
exp := int64(0)
div := big.NewInt(10)
for ; ; exp++ {
if absVal.Sign() == 0 {
break
}
absVal = absVal.Div(absVal, div)
}
expVal := exp + int64(d.Exponent())
expSign := codecSign(expVal)
// For negtive exp, do bit reverse for exp.
// For negtive decimal, do bit reverse for exp and value.
expVal = encodeExp(expVal, expSign, valSign)
codecValue(value, valSign)
r := EncodeInt(b, valSign)
r = EncodeInt(r, expSign)
r = EncodeInt(r, expVal)
r = EncodeBytes(r, value)
return r
}
示例3: EncodeDecimal
// EncodeDecimal encodes a decimal d into a byte slice which can be sorted lexicographically later.
// EncodeDecimal guarantees that the encoded value is in ascending order for comparison.
// Decimal encoding:
// Byte -> value sign
// EncodeInt -> exp value
// EncodeBytes -> abs value bytes
func EncodeDecimal(b []byte, d mysql.Decimal) []byte {
if d.Equals(mysql.ZeroDecimal) {
return append(b, byte(zeroSign))
}
v := d.BigIntValue()
valSign := codecSign(int64(v.Sign()))
absVal := new(big.Int)
absVal.Abs(v)
// Get exp and value, format is "value":"exp".
// like "12.34" -> "0.1234":"2".
// like "-0.01234" -> "-0.1234":"-1".
exp := int64(0)
div := big.NewInt(10)
mod := big.NewInt(0)
value := []byte{}
for ; ; exp++ {
if absVal.Sign() == 0 {
break
}
mod.Mod(absVal, div)
absVal.Div(absVal, div)
value = append([]byte(strconv.Itoa(int(mod.Int64()))), value...)
}
value = bytes.TrimRight(value, "0")
expVal := exp + int64(d.Exponent())
if valSign == negativeSign {
expVal = -expVal
}
b = append(b, byte(valSign))
b = EncodeInt(b, expVal)
if valSign == negativeSign {
b = EncodeBytesDesc(b, value)
} else {
b = EncodeBytes(b, value)
}
return b
}