Created
April 11, 2018 15:30
-
-
Save edwinlab/f5f3c83016fe2a9b345488d27d8a02be to your computer and use it in GitHub Desktop.
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" | |
| ) | |
| type usd int64 | |
| func USD(f float64) usd { | |
| return usd((f * 100) + 0.5) | |
| } | |
| func (m usd) float() float64 { | |
| x := float64(m) | |
| x = x / 100 | |
| return x | |
| } | |
| func (m usd) multiply(f float64) usd { | |
| x := (float64(m) * f) + 0.5 | |
| return usd(x) | |
| } | |
| func (m usd) String() string { | |
| x := float64(m) | |
| x = x / 100 | |
| return fmt.Sprintf("$%.2f", x) | |
| } | |
| func main() { | |
| fmt.Println("Product costs $9.09. Tax is 9.75%.") | |
| f := 9.09 | |
| t := 0.0975 | |
| ft := f * t | |
| fmt.Printf("Floats: %.18f * %.18f = %.18f\n", f, t, ft) | |
| u := USD(9.09) | |
| ut := u.multiply(t) | |
| fmt.Printf("USD: %v * %v = %v\n", u, t, ut) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment