LoginSignup
9
5

More than 5 years have passed since last update.

[Go]小数点第Nで四捨五入する

Last updated at Posted at 2019-03-29

概要

小数点第2、第3...と四捨五入する方法。

floatからstring

sample.go
v := fmt.Sprintf("%.2f", 12.3456)
fmt.Println(v) // "12.35"

v = fmt.Sprintf("%.3f", 12.3456)
fmt.Println(v) // "12.346"

floatからfloat

sample.go
v := 12.3456
fmt.Println(math.Round(v*100) / 100)   // 12.35
fmt.Println(math.Round(v*1000) / 1000) // 12.346

参考

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5