Commit 0eaf471d by zhengqiuyun86

float精度计算

parent ffa56339
package util
import (
"fmt"
"math/big"
"strconv"
)
type numberUtilStruct struct {
}
var NumberUtil = &numberUtilStruct{}
func (s *numberUtilStruct) Float64Format(p float64, digit int) float64 {
format := "%." + strconv.Itoa(digit) + "f"
value, _ := strconv.ParseFloat(fmt.Sprintf(format, p), 64)
return value
}
type bigDecimalStruct struct {
}
var BigDecimal = &bigDecimalStruct{}
// Add 精度加法 a+b
func (s *bigDecimalStruct) Add(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Add(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
return &r
}
// Sub 精度减法 a-b
func (s *bigDecimalStruct) Sub(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Sub(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
return &r
}
// Mul 精度乘法 axb
func (s *bigDecimalStruct) Mul(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Mul(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
return &r
}
// Quo 精度除法 a/b
func (s *bigDecimalStruct) Quo(a *float64, b *float64) *float64 {
r, _ := new(big.Float).Quo(new(big.Float).SetFloat64(*a), new(big.Float).SetFloat64(*b)).Float64()
return &r
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment