Skip to content

Instantly share code, notes, and snippets.

@kenenbek
Created July 20, 2018 05:57
Show Gist options
  • Save kenenbek/c56183f936caf68fdbff54abf4e60516 to your computer and use it in GitHub Desktop.
Save kenenbek/c56183f936caf68fdbff54abf4e60516 to your computer and use it in GitHub Desktop.
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