bc

How to get bc to handle numbers in scientific (aka exponential) notation?

本小妞迷上赌 提交于 2019-11-27 05:41:49
问题 bc doesn't like numbers expressed in scientific notation (aka exponential notation). $ echo "3.1e1*2" | bc -l (standard_in) 1: parse error but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to understand exponential notation? If not, what can I do to translate them into a format that bc will understand? 回答1: Unfortunately, bc doesn't support scientific notation. However, it can be translated into a format that bc can handle, using

How do I get bc(1) to print the leading zero?

北城以北 提交于 2019-11-26 09:24:32
问题 I do something like the following in a Makefile: echo \"0.1 + 0.1\" | bc (in the real file the numbers are dynamic, of course) It prints .2 but I want it to print 0.2 . I would like to do this without resorting to sed but I can\'t seem to find how to get bc to print the zero. Or is bc just not able to do this? 回答1: You can also resort to awk to format: echo "0.1 + 0.1" | bc | awk '{printf "%f", $0}' or with awk itself doing the math: echo "0.1 0.1" | awk '{printf "%f", $1 + $2}' 回答2: This