How to convert float to integer number

How to convert float to integer number

In this tutorial we will learn how to convert float to integer number.
This tutorial is helpful in bash or shell scripting.

For eg. the float value number is 204.47584 and you would like to show only number in integer form means upto 204. So for this use the given below steps.

[root@server ~]# FLOAT=204.47584
[root@server ~]# INT=${FLOAT/.*}
[root@server ~]# echo $INT
204
[root@server ~]#

Now I can use this logic in bash script.
In given below example, I am monitoring Java command when consumes more than 90% it will send me email to my email-id.

This is just for an example,you can make it better with putting some more logic.

#!/bin/bash
## convert float to integer number

xyz=`ps -eo pcpu,user,comm,pid | sort -k 1 -r | head -2|grep java`
FLOAT=`ps -eo pcpu,user,comm | sort -k 1 -r | head -2|grep java|awk ‘{print $1}’`
INT=${FLOAT/.*}

if [ $INT -gt 90 ]
then
echo “CPU usage by mysql server is $INT, Report: $xyz”|mail -s “java cpu usage in percentage” joe@example.com
else
exit
fi

Leave a Comment

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