(val float64, decimalPlaces int, padToDecimalPlaces int)
| 101 | } |
| 102 | |
| 103 | func 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/ |
| 123 | func IntToBase2Byte(size int) string { |
no outgoing calls