如何为 bc 计算器设置默认比例?

Bul*_*ush 12 command-line bc

Ubuntu 14.04.1 LTS

如何设置 bc 计算器的默认比例?每次运行 bc 时,我希望 scale=2 为默认值,我想将所有计算限制为 2 个小数位。我在我的主目录中创建了一个文件,.bc并在其中放置scale=2了第一行,然后是回车。

上的权限〜/ .BC是:-rw-rw-rw-。那正确吗?

然后我做到了set BC_ENV_ARGS=~/.bc; export BC_ENV_ARGS。然后我跑bc,做了8.37843*32.190233之类的测试,还是得到了2位小数。

在线手册没有提供任何关于这样做的例子,所以请不要引导我去那里。

谢谢。

编辑:好的,当我做像 78/31 这样的测试时,它给了我 2 位小数。但是当我做上面的测试时,它给了我超过 2 位小数。这是为什么?我总是想只显示 2 位小数。

Cha*_*Kim 13

正如穆鲁所说,结果的规模是所涉及的表达式的最大规模。但是如果要设置除法的比例(要设置截断级别),请放置一个文件.bc in your home (ex. /home/yourid/.bc)并对其进行编辑以包含(文件名可以是任何内容)

scale=8  (whatever you want)
Run Code Online (Sandbox Code Playgroud)

然后在你的 .cshrc 文件中放入

setenv BC_ENV_ARGS '/home/yourid/.bc'
Run Code Online (Sandbox Code Playgroud)

这样,您的默认比例设置为 8。

bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1/3
.33333333
Run Code Online (Sandbox Code Playgroud)


mur*_*uru 1

来自man bc(强调我的):

Unless  specifically  mentioned  the scale of the result is the maximum
scale of the expressions involved.
....
expr / expr
      The result  of  the  expression  is  the  quotient  of  the  two
      expressions.   The  scale  of  the  result  is  the value of the
      variable scale.
Run Code Online (Sandbox Code Playgroud)

进一步阅读,它似乎scale主要只适用于涉及除法的情况(/^带有负指数,%等)。

因此,要么使用其他工具按照您想要的方式打印它(例如printfawk),要么除以 1:

$ echo '8.37843*32.190233/1' | bc
269.70
Run Code Online (Sandbox Code Playgroud)