package main /** * A program to implement dollars and euros * With good printing for each and conversions between * Created Sep 19, 2023 * gtowell */ import "fmt" // a type for dollars type dollars float64 // toString method for dollars func (this dollars) String() string { return fmt.Sprintf("$%.2f", this) } // Convert dollars to euros func (this dollars) toEuros() euros { return euros(float64(this) / 1.07) } // A type for euros type euros float64 // toString method for euros func (this euros) String() string { return fmt.Sprintf("€%0.2f", this) } // convert euros to dollars func (this euros) toDollars() dollars { return dollars(this * euros(1.07)); } // using the euro and dollar types func main() { aa := 7.2128731278 dol := dollars(aa) // just cast float 64 to $ eur := dol.toEuros() // convert $ to € using the method on dollars fmt.Printf("float64 %v\n", aa) fmt.Printf("dollars %v\n", dol) fmt.Printf("euros %v\n", eur) }