在bash脚本中将前导零添加到浮点数 [英] Adding a leading zero to a float number in a bash script

查看:56
本文介绍了在bash脚本中将前导零添加到浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本

#!/bin/bash
echo -n "number 1 :";
read number1
echo -n "number 2 :";
read number2    
jlh=$(echo $number1 + $number2 | bc -l | sed 's/^\./0./');
    echo "your result : $number1 + $number2 = $jlh "

如果数字1的输入为 -1 ,数字2的输入为 0.9 ,为什么结果仅为-.1 .

if input for number 1 is -1 , and number 2 is 0.9, why the result only -.1.

我想这样显示零.

Your result : -1 + 0.9 = -0.1

我该怎么做?

推荐答案

因为您现在只考虑情况 .NNN ,而不是-.NNN ,在前面加上减号-:

Because you by now just consider the case .NNN, but not the -.NNN, that is having the minus - sign before:

通过它应该可以工作:

sed    -e 's/^\./0./'       -e 's/^-\./-0./'
           start with .         start with -.

在一起;

jlh=$(echo $number1 + $number2 | bc -l | sed -e 's/^\./0./' -e 's/^-\./-0./');

这篇关于在bash脚本中将前导零添加到浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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