Skip to content

Instantly share code, notes, and snippets.

@edwinlab
Created April 11, 2018 15:30
Show Gist options
  • Select an option

  • Save edwinlab/f5f3c83016fe2a9b345488d27d8a02be to your computer and use it in GitHub Desktop.

Select an option

Save edwinlab/f5f3c83016fe2a9b345488d27d8a02be to your computer and use it in GitHub Desktop.
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