无法在外壳脚本中获得浮动值作为输出

发布于 2025-02-13 13:41:05 字数 392 浏览 0 评论 0原文

当我尝试像您可以在下面的代码段中看到的值分配值时,当值进入浮点时,它并没有向我显示浮点吗?它只是打印0。我正在对MB进行GB转换。 大约2-3个值的转换

任何人都可以帮助我让我看到 0之后的

echo -e "Enter your value in MB! \c"
read -r userINPUT

。 >转换结果:

echo "In GB it is: $((userINPUT / 1024))"

输出我得到:

Enter your Frist Number! 25
In GB it is: 0

When I try to divide values like you can see in below code snippet when value gets in float it doesn't shows me the float? It just prints 0. I'm doing MB to GB conversion. Anybody can help me to let me see the conversion to some 2-3 values after 0. like 0.2545

Taking an input from user:

echo -e "Enter your value in MB! \c"
read -r userINPUT

Converted result:

echo "In GB it is: $((userINPUT / 1024))"

Output I'm getting:

Enter your Frist Number! 25
In GB it is: 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2025-02-20 13:41:05

bash不支持浮点算术。

您可以使用BC命令对浮点值进行操作。

喜欢:

bc <<< "scale=2;  $userINPUT / 1024 "

或者更好,您可以安装和使用命令 numfmt 转换

https://man7.org/linux/man-pages/man1/numfmt.1.html

Bash does not support floating point arithmetic.

You can use bc command to do the operations on float values.

Like:

bc <<< "scale=2;  $userINPUT / 1024 "

Or better you can install and use the command numfmt for the conversion

https://man7.org/linux/man-pages/man1/numfmt.1.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文