package main import ( "fmt" "math/big" ) func main() { r1 := big.NewRat(1, 2) // 1/2 r2 := big.NewRat(3, 4) // 3/4 r1.Add(r1, r2) // add r2 to r1 fmt.Println(r1.FloatString(2)) // output: 1.25 }
package main import ( "fmt" "math/big" ) func main() { r := big.NewRat(2, 7) // 2/7 i := 5 r.Add(r, big.NewRat(int64(i), 1)) // add i to r fmt.Println(r.FloatString(2)) // output: 2.71 }In this example, we create a Rat value representing 2/7 and an int value of 5, then add the int value to the Rat value using the Add method. The result should be 2.71 which is outputted using the FloatString method. Package library: math/big