围捕浮点数字的bash [英] Rounding up float point numbers bash
本文介绍了围捕浮点数字的bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好了,所以我试图围捕 17.92857
,所以它得到的输入 17.929 $ C $的输入C>在bash。
Ok, so I'm trying to round up an input of 17.92857
, so that it gets an input of 17.929
in bash.
我的code到目前为止是:
My code so far is:
read input
echo "scale = 3; $input" | bc -l
然而,当我用这个,它不圆了,它返回 17.928
。
有谁知道任何解决这个?
Does anyone know any solutions to this?
推荐答案
在案件输入
包含一个数字,没有必要像<$ C $外部命令C> BC 。你可以只用的printf
:
In case input
contains a number, there is no need for an external command like bc
. You can just use printf
:
printf "%.3f\n" "$input"
编辑:如果输入的是一个公式,你应该使用,无论 BC
如下面的命令之一:
In case the input is a formula, you should however use bc
as in one of the following commands:
printf "%.3f\n" $(bc -l <<< "$input")
printf "%.3f\n" $(echo "$input" | bc -l)
这篇关于围捕浮点数字的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文