github.com/condensat/bank-core@v0.1.0/database/utils/fixed.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package utils 6 7 import ( 8 "math" 9 "math/big" 10 "strconv" 11 ) 12 13 func Round(num float64) int { 14 return int(num + math.Copysign(0.5, num)) 15 } 16 17 func RoundUnit(x, unit float64) float64 { 18 return float64(int64(x/unit+0.5)) * unit 19 } 20 21 func ToFixed(num float64, precision int) float64 { 22 if precision < 0 { 23 precision = 0 24 } 25 26 var f big.Float 27 f.SetMode(big.AwayFromZero).SetFloat64(num) 28 str := f.Text('f', precision) 29 30 round, err := strconv.ParseFloat(str, 64) 31 if err != nil { 32 return num // return original value 33 } 34 return round 35 }