本文整理汇总了Golang中github.com/pingcap/tidb/util/types.ToDecimal函数的典型用法代码示例。如果您正苦于以下问题:Golang ToDecimal函数的具体用法?Golang ToDecimal怎么用?Golang ToDecimal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToDecimal函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: evalIntDiv
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div
func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return x / y, nil
case uint64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return uint64(x) / y, nil
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x / uint64(y), nil
case uint64:
if y == 0 {
return nil, nil
}
return x / y, nil
}
}
// if any is none integer, use decimal to calculate
x, err := types.ToDecimal(a)
if err != nil {
return nil, o.traceErr(err)
}
y, err := types.ToDecimal(b)
if err != nil {
return nil, o.traceErr(err)
}
if f, _ := y.Float64(); f == 0 {
return nil, nil
}
return x.Div(y).IntPart(), nil
}
示例2: evalIntDiv
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div
func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return types.DivInt64(x, y)
case uint64:
if y == 0 {
return nil, nil
}
return types.DivIntWithUint(x, y)
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return types.DivUintWithInt(x, y)
case uint64:
if y == 0 {
return nil, nil
}
return x / y, nil
}
}
// if any is none integer, use decimal to calculate
x, err := types.ToDecimal(a)
if err != nil {
return nil, o.traceErr(err)
}
y, err := types.ToDecimal(b)
if err != nil {
return nil, o.traceErr(err)
}
if f, _ := y.Float64(); f == 0 {
return nil, nil
}
return x.Div(y).IntPart(), nil
}
示例3: computeIntDiv
func computeIntDiv(a, b interface{}) (interface{}, error) {
switch x := a.(type) {
case int64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return types.DivInt64(x, y)
case uint64:
if y == 0 {
return nil, nil
}
return types.DivIntWithUint(x, y)
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
return types.DivUintWithInt(x, y)
case uint64:
if y == 0 {
return nil, nil
}
return x / y, nil
}
}
// if any is none integer, use decimal to calculate
x, err := types.ToDecimal(a)
if err != nil {
return nil, err
}
y, err := types.ToDecimal(b)
if err != nil {
return nil, err
}
if f, _ := y.Float64(); f == 0 {
return nil, nil
}
return x.Div(y).IntPart(), nil
}
示例4: evalDiv
func (o *BinaryOperation) evalDiv(a interface{}, b interface{}) (interface{}, error) {
// MySQL support integer divison Div and division operator /
// we use opcode.Div for division operator and will use another for integer division later.
// for division operator, we will use float64 for calculation.
switch x := a.(type) {
case float64:
y, err := types.ToFloat64(b)
if err != nil {
return nil, err
}
if y == 0 {
return nil, nil
}
return x / y, nil
default:
// the scale of the result is the scale of the first operand plus
// the value of the div_precision_increment system variable (which is 4 by default)
// we will use 4 here
xa, err := types.ToDecimal(a)
if err != nil {
return nil, o.traceErr(err)
}
xb, err := types.ToDecimal(b)
if err != nil {
return nil, o.traceErr(err)
}
if f, _ := xb.Float64(); f == 0 {
// division by zero return null
return nil, nil
}
return xa.Div(xb), nil
}
}