github.com/blend/go-sdk@v1.20220411.3/mathutil/round_places.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package mathutil 9 10 import "math" 11 12 // RoundPlaces a float to a specific decimal place or precision 13 func RoundPlaces(input float64, places int) float64 { 14 if math.IsNaN(input) { 15 return 0.0 16 } 17 18 sign := 1.0 19 if input < 0 { 20 sign = -1 21 input *= -1 22 } 23 24 rounded := float64(0) 25 precision := math.Pow(10, float64(places)) 26 digit := input * precision 27 _, decimal := math.Modf(digit) 28 29 if decimal >= 0.5 { 30 rounded = math.Ceil(digit) 31 } else { 32 rounded = math.Floor(digit) 33 } 34 35 return rounded / precision * sign 36 }