本文整理匯總了Golang中github.com/pingcap/tidb/util/types.Datum.ToDecimal方法的典型用法代碼示例。如果您正苦於以下問題:Golang Datum.ToDecimal方法的具體用法?Golang Datum.ToDecimal怎麽用?Golang Datum.ToDecimal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pingcap/tidb/util/types.Datum
的用法示例。
在下文中一共展示了Datum.ToDecimal方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: computeDiv
func computeDiv(a, b types.Datum) (d types.Datum, err 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 a.Kind() {
case types.KindFloat64:
y, err1 := b.ToFloat64()
if err1 != nil {
return d, errors.Trace(err1)
}
if y == 0 {
return d, nil
}
x := a.GetFloat64()
d.SetFloat64(x / y)
return d, 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, err1 := a.ToDecimal()
if err != nil {
return d, errors.Trace(err1)
}
xb, err1 := b.ToDecimal()
if err1 != nil {
return d, errors.Trace(err1)
}
if f, _ := xb.Float64(); f == 0 {
// division by zero return null
return d, nil
}
d.SetMysqlDecimal(xa.Div(xb))
return d, nil
}
}
示例2: computeIntDiv
func computeIntDiv(a, b types.Datum) (d types.Datum, err error) {
switch a.Kind() {
case types.KindInt64:
x := a.GetInt64()
switch b.Kind() {
case types.KindInt64:
y := b.GetInt64()
if y == 0 {
return d, nil
}
r, err1 := types.DivInt64(x, y)
d.SetInt64(r)
return d, errors.Trace(err1)
case types.KindUint64:
y := b.GetUint64()
if y == 0 {
return d, nil
}
r, err1 := types.DivIntWithUint(x, y)
d.SetUint64(r)
return d, errors.Trace(err1)
}
case types.KindUint64:
x := a.GetUint64()
switch b.Kind() {
case types.KindInt64:
y := b.GetInt64()
if y == 0 {
return d, nil
}
r, err1 := types.DivUintWithInt(x, y)
d.SetUint64(r)
return d, errors.Trace(err1)
case types.KindUint64:
y := b.GetUint64()
if y == 0 {
return d, nil
}
d.SetUint64(x / y)
return d, nil
}
}
// if any is none integer, use decimal to calculate
x, err := a.ToDecimal()
if err != nil {
return d, errors.Trace(err)
}
y, err := b.ToDecimal()
if err != nil {
return d, errors.Trace(err)
}
if f, _ := y.Float64(); f == 0 {
return d, nil
}
d.SetInt64(x.Div(y).IntPart())
return d, nil
}