0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】小数点以下を表示する方法 .to_f

Posted at

背景

平均値を小数以下まで表示させたいシュチュエーションがあったので、調べてみました。

10÷3を小数点第3位まで出したい時

a = 10
b = 3
c = a.to_f / b.to_f

puts c.round(3)

=> 3.333

.to_f

文字列を10進数と解釈して、浮動小数点数Floatに変換するメソッドです。

.round()

引数に少数点の桁数を指定することができ、四捨五入して表示されます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?