9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2019-03-29

追記

この記事の方法では丸め誤差がでるため、以下の記事を参考にしてください。
https://qiita.com/fujiwaram/items/f877a03114a1a47adc7d

概要

小数点第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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?