In this lecture, we cover an important difference in Go between “round a number” and “print a number nicely”: formatting via
fmt.
Printf(
"%.2f",
x) does not change the variable’s value, while mathematical rounding via
math.
Round(
x) returns a new value. We will look at formats
%f,
%g, rounding with
Round/
Floor/
Ceil, the “scale → round → scale back” technique, where rounding belongs in a program, and typical mistakes when working with
float64.