我想丢弃浮点精度。
(4.43597).discard(3) => 4.435 (not 4.436)
我不想用圆形。 我该怎么办? 我找不到办法。
答案 0 :(得分:10)
答案 1 :(得分:3)
仍然使用round
,但具有截断所需位置的效果。
(4.43597-0.0005).round(3)
答案 2 :(得分:0)
有点矫枉过正,但是,
>> (4.43597).to_s.scan(/\d+\.\d{3}/)[0].to_f
=> 4.435
答案 3 :(得分:0)
另一种选择:
1.乘以1000
2.使用floor()向下舍入
3.除以1000.0