如何隐藏或在我的bash脚本删除零?

编程入门 行业动态 更新时间:2024-10-27 06:25:27
本文介绍了如何隐藏或在我的bash脚本删除零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的脚本:

echo -n "number 1 :"; read bil1 echo -n "number 2 :"; read bil2 multiple=$(echo $bil1*$bil2 |bc -l |sed -e 's/^\./0./' -e 's/^-\./-0./'); echo " Your result : $bil1 * $bil2 = $multiple ";

如果我输入 9.5 为 $ bil1 和 300 为 $ bil2 ,结果是 2850.0 。

If I input 9.5 for $bil1 and 300 for $bil2, the result is 2850.0.

我想要的结果:

Your result : 9.5 * 300 = 2850

我如何隐藏或结果的末尾加上零?即只显示 2850 ?

推荐答案

以下部分添加到您的sed命令(在末尾):

Add the following part to your sed command (at the end):

-e 's/\.0*$//'

(删除一个点,然后在通过没有结束任意多个零)。

(Delete a dot followed by arbitrarily many zeroes at the end by nothing.)

顺便说一句,你最好更换回声$ bil1 * $ bil2 按回声$ bil1 * $ bil2,否则你可能会根据当前目录的内容得到奇怪的效果。

By the way, you better replace echo $bil1*$bil2 by echo "$bil1*$bil2", otherwise you may get strange effects depending on the contents of the current directory.

更多推荐

如何隐藏或在我的bash脚本删除零?

本文发布于:2023-10-09 10:26:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!