how to remove decimal from a variable?

前端 未结 2 512
情深已故
情深已故 2021-02-05 16:28

how to remove decimal place in shell script.i am multiplying MB with bytes to get value in bytes .I need to remove decimal place.

ex:-
196.3*1024*1024
205835468         


        
2条回答
  •  失恋的感觉
    2021-02-05 16:37

    (You did not mention what shell you're using; this answer assumes Bash).

    You can remove the decimal values using ${VAR%.*}. For example:

    [me@home]$ X=$(echo "196.3 * 1024 * 1024" | bc)
    [me@home]$ echo $X
    205835468.8
    [me@home]$ echo ${X%.*}
    205835468
    

    Note that this truncates the value rather than rounds it. If you wish to round it, use printf as shown in Roman's answer.

    The ${variable%pattern} syntax deletes the shortest match of pattern starting from tbe back of variable. For more information, read http://tldp.org/LDP/abs/html/string-manipulation.html

提交回复
热议问题