MCPcopy Create free account
hub / github.com/labstack/gommon / ParseDecimal

Method ParseDecimal

bytes/bytes.go:168–196  ·  view source on GitHub ↗

ParseDecimal parses human readable bytes string to bytes integer. For example, 6GB (6G is also valid) will return 6000000000.

(value string)

Source from the content-addressed store, hash-verified

166// ParseDecimal parses human readable bytes string to bytes integer.
167// For example, 6GB (6G is also valid) will return 6000000000.
168func (*Bytes) ParseDecimal(value string) (i int64, err error) {
169 parts := patternDecimal.FindStringSubmatch(value)
170 if len(parts) < 3 {
171 return 0, fmt.Errorf("error parsing value=%s", value)
172 }
173 bytesString := parts[1]
174 multiple := strings.ToUpper(parts[2])
175 bytes, err := strconv.ParseFloat(bytesString, 64)
176 if err != nil {
177 return
178 }
179
180 switch multiple {
181 case "K", "KB":
182 return int64(bytes * KB), nil
183 case "M", "MB":
184 return int64(bytes * MB), nil
185 case "G", "GB":
186 return int64(bytes * GB), nil
187 case "T", "TB":
188 return int64(bytes * TB), nil
189 case "P", "PB":
190 return int64(bytes * PB), nil
191 case "E", "EB":
192 return int64(bytes * EB), nil
193 default:
194 return int64(bytes), nil
195 }
196}
197
198// Format wraps global Bytes's Format function.
199func Format(value int64) string {

Callers 1

ParseMethod · 0.95

Calls 1

ErrorfMethod · 0.80

Tested by

no test coverage detected