Last active
August 29, 2015 14:04
-
-
Save peterhellberg/20fb92998a9bc3a85583 to your computer and use it in GitHub Desktop.
Percentage Calculators in Go
This file contains hidden or 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" | |
| "os" | |
| "strconv" | |
| ) | |
| func main() { | |
| if len(os.Args) == 3 { | |
| v1 := getFloat(1) | |
| v2 := getFloat(2) | |
| fmt.Println(((v2 - v1) / ((v2 + v1) / 2.0)) * 100.0) | |
| } | |
| } | |
| func getFloat(n int) float64 { | |
| f, err := strconv.ParseFloat(os.Args[n], 64) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| return f | |
| } |
This file contains hidden or 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" | |
| "os" | |
| "strconv" | |
| ) | |
| func main() { | |
| if len(os.Args) == 3 { | |
| v1 := getFloat(1) | |
| v2 := getFloat(2) | |
| fmt.Println((v1 / v2) * 100.0) | |
| } | |
| } | |
| func getFloat(n int) float64 { | |
| f, err := strconv.ParseFloat(os.Args[n], 64) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| return f | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment