bash 将浮点变量转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1362298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert floating point variable to integer?
提问by shantanuo
The shell script shown below will show a warning if the page takes more than 6 seconds to load. The problem is that the myduration
variable is not an integer. How do I convert it to integer?
如果页面加载时间超过 6 秒,下面显示的 shell 脚本将显示警告。问题是myduration
变量不是整数。如何将其转换为整数?
myduration=$(curl http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}) > /dev/null ; \
[[ $myduration -gt 1 ]] && echo "`date +'%y%m%d%H%M%S'
It took more than 6 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1
页面加载时间超过6秒 http://192.168.50.1/mantisbt/view.php?id=1
回答by Shizzmo
Assuming $myduration is a decimal or integer
假设 $myduration 是小数或整数
$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6
回答by Brijesh Valera
You can do this:
你可以这样做:
float=1.23
int=${float%.*}
I am using this on bash.
我在 bash 上使用它。
回答by Paused until further notice.
Eliminate page contents from the variable:
从变量中删除页面内容:
When I tried your command, myduration
contained the HTML contents of the page at the URL I used in my test plus the time value. By adding -s
to suppress the progress bar and adding -o /dev/null
to the options for curl
, I was able to remove the redirect to /dev/null
and have only the time saved in myduration
.
当我尝试您的命令时,myduration
包含我在测试中使用的 URL 处的页面 HTML 内容以及时间值。通过添加-s
以抑制进度条并添加-o /dev/null
到 的选项curl
,我能够删除重定向到/dev/null
并且只将时间保存在myduration
.
Since the value of myduration
is likely to be short, you can use the technique ire_and_curses
shows which will often yield zero as its result which would be less than the 1 you are testing for (note that your log message says "6 seconds", though).
由于 的值myduration
可能很短,您可以使用ire_and_curses
显示通常会产生零作为其结果的技术,这将小于您正在测试的 1(但请注意,您的日志消息显示“6 秒”)。
Finer resolution:
更精细的分辨率:
If you'd like to have a finer resolution test, you can multiply myduration
by 1000 using a technique like this:
如果您想进行更精细的分辨率测试,可以myduration
使用如下技术乘以1000:
mult1000 () {
local floor=${1%.*}
[[ $floor = "0" ]] && floor=''
local frac='0000'
[[ $floor != ]] && frac=${1#*.}$frac
echo ${floor}${frac:0:3}
}
Edit:This version of mult1000
properly handles values such as "0.234", "1", "2.", "3.5"
and "6.789". For values with more than three decimal places, the extra digits are truncated without rounding regardless of the value ("1.1119" becomes "1.111").
编辑:此版本mult1000
正确处理诸如“0.234”、“1”、“2.”、“3.5”和“6.789”之类的值。对于超过三个小数位的值,无论值如何,额外的数字都会被截断而不舍入(“1.1119”变为“1.111”)。
Your script with the changes I mentioned above and using mult1000
(with my own example time):
您的脚本进行了我上面提到的更改并使用了mult1000
(使用我自己的示例时间):
myduration=$(curl -s -o /dev/null http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}); [[ $(mult1000 $myduration) -gt 3500 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 3.5 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt
Here it is broken into multiple lines (and simplified) to make it more readable here in this answer:
在这里它被分成多行(并简化),以使其在此答案中更具可读性:
myduration=$(curl -s -o /dev/null http://example.com -w %{time_total})
[[ $(mult1000 $myduration) -gt 3500 ]] &&
echo "It took more than 3.5 seconds to load thttp://example.com" >> report.txt
回答by ire_and_curses
It's not entirely clear, but I thinkyou're asking how to convert a floating-point value (myduration
) to an integer in bash
. Something like this may help you, depending on which way you want to round your number.
这并不完全清楚,但我认为您是在问如何将浮点值 ( myduration
) 转换为bash
. 像这样的事情可能会对您有所帮助,具体取决于您想以哪种方式对数字进行四舍五入。
#!/bin/bash
floor_val=
ceil_val=
function floor() {
float_in=
floor_val=${float_in/.*}
}
function ceiling() {
float_in=
ceil_val=${float_in/.*}
ceil_val=$((ceil_val+1))
}
float_val=
echo Passed in: $float_val
floor $float_val
ceiling $float_val
echo Result of floor: $floor_val
echo Result of ceiling: $ceil_val
Example usage:
用法示例:
$ ./int.sh 12.345
Passed in: 12.345
Result of floor: 12
Result of ceiling: 13