Bash 把浮点(float)转化成整数(int)

第一种方法, 根据这篇文章所说:

Shell 中的花括号的特殊用法

float=1.23
int=${float%.*}

 

第二种方法: 使用bc 命令

$ echo "($float+0.5)/1" | bc

第三种方法: 使用 printf

$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.