如何让 bc(1) 打印前导零? [英] How do I get bc(1) to print the leading zero?

查看:13
本文介绍了如何让 bc(1) 打印前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Makefile 中执行以下操作:

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)

它打印 .2 但我希望它打印 0.2.

It prints .2 but I want it to print 0.2.

我想在不求助于 sed 的情况下做到这一点,但我似乎无法找到如何让 bc 打印零.还是 bc 无法做到这一点?

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?

推荐答案

你也可以借助 awk 来格式化:

You can also resort to awk to format:

 echo "0.1 + 0.1" | bc | awk '{printf "%f", $0}'

或者用 awk 自己做数学:

or with awk itself doing the math:

 echo "0.1 0.1" | awk '{printf "%f", $1 + $2}'

这篇关于如何让 bc(1) 打印前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆