//绝对值转成浮点数 func toFloat64(d decimal.Decimal) float64 { result, ok := d.Float64() if !ok { // TODO } return result }
//Decode is the inverse operation of Encode. //Decode returns latitude, longitude, and whether or not they are both represented precisely as float64 types. func Decode(bucket int64) (float64, float64, bool) { var latitudeUnshifted, longitudeUnshifted decimal.Decimal var latitude, longitude float64 var err error var exact bool bucketString := strconv.FormatInt(bucket, 10) for len(bucketString) < 18 { bucketString = "0" + bucketString } latString, lonString := unzip(bucketString) latString = latString[0:3] + "." + latString[3:] lonString = lonString[0:3] + "." + lonString[3:] latitudeUnshifted, err = decimal.NewFromString(latString) longitudeUnshifted, err = decimal.NewFromString(lonString) if err != nil { fmt.Errorf("Error creating decimal from string") } latitudeUnshifted = latitudeUnshifted.Sub(decimal.NewFromFloat(90.0)) longitudeUnshifted = longitudeUnshifted.Sub(decimal.NewFromFloat(180.0)) latitude, exact = latitudeUnshifted.Float64() longitude, exact = longitudeUnshifted.Float64() return latitude, longitude, exact }
func DecimalToString(res decimal.Decimal, min int32, max int32) string { if res.Cmp(decimal.New(10, min)) <= 0 || res.Cmp(decimal.New(10, max)) >= 0 { f, _ := res.Float64() return strconv.FormatFloat(f, 'G', -1, 64) } return res.String() }
func ConvertToDecimalBinary(f func(float64, float64) float64, a decimal.Decimal, b decimal.Decimal) decimal.Decimal { aFloat, _ := a.Float64() bFloat, _ := b.Float64() return decimal.NewFromFloat(f(aFloat, bFloat)) }