Bash Division保持0 [重复](Bash Division Keeps giving 0 [duplicate])

编程入门 行业动态 更新时间:2024-10-28 07:32:42
Bash Division保持0 [重复](Bash Division Keeps giving 0 [duplicate])

这个问题在这里已有答案:

如何在bash中使用浮点除法? 11个答案

我知道bash不支持长时间划分,这个问题可能会被归结为地狱,但在这里它会发生。 我查找了一个bash work-around给了我一个假设的数值。 例如,这就是我得到的:

#!/bin/bash SMALL_NUMBER=4 LARGE_NUMBER=56100 MATH_PROBLEM=$((100*$SMALL_NUMBER/$LARGE_NUMBER)) echo $MATH_PROBLEM

我执行代码,输出总是0.我认为将表达式乘以100将解决这个问题,但显然不是。 有人能告诉我这是怎么回事吗?

This question already has an answer here:

How do I use floating-point division in bash? 14 answers

I know bash doesn't support long division and this question is probably going to be downvoted to hell, but here it goes. I looked up a bash work-around to giving me a supposed numeric value for the division. For example, this is what I got:

#!/bin/bash SMALL_NUMBER=4 LARGE_NUMBER=56100 MATH_PROBLEM=$((100*$SMALL_NUMBER/$LARGE_NUMBER)) echo $MATH_PROBLEM

I execute the code and the output is always 0. I thought multiplying the expression by 100 would fix this but apparently not. Can someone tell me what is going on with this?

最满意答案

Bash只做整数,而不是浮点数; 您必须将任务委派给诸如bc的工具

Bash only does integers, not floats; you must delegate the task to a tool such as bc

更多推荐

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

发布评论

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

>www.elefans.com

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