Created
February 7, 2015 18:22
-
-
Save kittinunf/08654a00c676b8f339a5 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 main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
fmt.Println(customParseToFloat64("-12.345678")) | |
} | |
func customParseToFloat64(input string) (result float64) { | |
powerOfTen := []int{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000} | |
pointPosition := -1 | |
n := 0.0 | |
numericCount := 0 | |
reversal := 1 | |
if strings.HasPrefix(input, "-") { | |
reversal = -1 | |
input = input[1:len(input)] | |
} | |
for index, value := range input { | |
if value == '.' { | |
pointPosition = index | |
} else { | |
distance := float64(value - '0') | |
n = (n * 10) + distance | |
numericCount++ | |
} | |
} | |
if pointPosition == -1 { | |
result = n | |
} else { | |
result = n / float64(powerOfTen[numericCount-pointPosition]) | |
} | |
result = result * float64(reversal) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment