MCPcopy
hub / github.com/cortexlabs/cortex / Round

Function Round

pkg/lib/strings/stringify.go:103–120  ·  view source on GitHub ↗
(val float64, decimalPlaces int, padToDecimalPlaces int)

Source from the content-addressed store, hash-verified

101}
102
103func Round(val float64, decimalPlaces int, padToDecimalPlaces int) string {
104 rounded := math.Round(val*math.Pow10(decimalPlaces)) / math.Pow10(decimalPlaces)
105 str := strconv.FormatFloat(rounded, 'f', -1, 64)
106 if padToDecimalPlaces == 0 {
107 return str
108 }
109 split := strings.Split(str, ".")
110 intVal := split[0]
111 decVal := ""
112 if len(split) > 1 {
113 decVal = split[1]
114 }
115 if len(decVal) >= padToDecimalPlaces {
116 return str
117 }
118 numZeros := padToDecimalPlaces - len(decVal)
119 return intVal + "." + decVal + strings.Repeat("0", numZeros)
120}
121
122// copied from https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
123func IntToBase2Byte(size int) string {

Callers 4

DollarsAndCentsFunction · 0.85
DollarsAndTenthsOfCentsFunction · 0.85
DollarsMaxPrecisionFunction · 0.85
TestRoundFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestRoundFunction · 0.68