You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
664 B
30 lines
664 B
package conver |
|
|
|
import ( |
|
"fmt" |
|
"github.com/dsnet/golib/unitconv" |
|
) |
|
|
|
// ParseDataUnit parse 1Ki -> 1024, 1K -> 1000 |
|
func ParseDataUnit(unit string) (val float64, err error) { |
|
val, err = unitconv.ParsePrefix(unit, unitconv.AutoParse) |
|
return |
|
} |
|
|
|
func ParseDataUnitInt(unit string) (val int, err error) { |
|
v, err := ParseDataUnit(unit) |
|
val = int(v) |
|
return |
|
} |
|
|
|
func MustParseDataUnit(unit string) (val float64) { |
|
val, err := unitconv.ParsePrefix(unit, unitconv.AutoParse) |
|
if err != nil { |
|
panic(fmt.Errorf("error parse data unit %s, %s", unit, err.Error())) |
|
} |
|
return |
|
} |
|
|
|
func MustParseDataUnitInt(unit string) (val int) { |
|
return int(MustParseDataUnit(unit)) |
|
}
|
|
|