Created
July 20, 2018 05:57
-
-
Save kenenbek/c56183f936caf68fdbff54abf4e60516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package src | |
import ( | |
"math" | |
"regexp" | |
"strings" | |
"strconv" | |
) | |
func createUnits() { | |
unitsMap := make(map[string]float64) | |
TERA := math.Pow10(12) | |
GIGA := math.Pow10(9) | |
MEGA := math.Pow10(6) | |
KILO := math.Pow10(3) | |
unitsMap["TB"] = TERA | |
unitsMap["GB"] = GIGA | |
unitsMap["MB"] = MEGA | |
unitsMap["KB"] = KILO | |
unitsMap["B"] = 1 | |
unitsMap["GBps"] = GIGA | |
unitsMap["MBps"] = MEGA | |
unitsMap["KBps"] = KILO | |
unitsMap["Bps"] = 1 | |
unitsMap["Gf"] = GIGA | |
unitsMap["Mf"] = MEGA | |
unitsMap["Kf"] = KILO | |
unitsMap["f"] = 1 | |
TATLIN.units = unitsMap | |
} | |
func UnitToFloatParser(value string) float64 { | |
re := regexp.MustCompile("\\d*\\.?\\d*") | |
numericValue := re.FindString(value) | |
unit := strings.Replace(value, numericValue, "", 1) | |
convertedNum, _ := strconv.ParseFloat(numericValue, 64) | |
multiplier, err := TATLIN.units[unit] | |
if !err { | |
panic("PARSED incorrectly") | |
} | |
return convertedNum * multiplier | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment