1834395 Members
1690 Online
110066 Solutions
New Discussion

decimals in shell script

 
Tony  LeBlanc
Occasional Contributor

decimals in shell script

Would someone know how to remove the decimal from a decimal number?

ex)

var=5.6
echo $var (only want 5 to show)

Thanks,
Tony
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: decimals in shell script

Hi Tony:

One way is to use 'awk' with the "." as the field delimiter:

# V=5.6
# echo $V|awk -F. '{print $1}'

Regards!

...JRF...
Curtis Larson
Trusted Contributor

Re: decimals in shell script

#!/usr/bin/ksh

integer i

var=5.6
i=$var
echo $i
Danny Engelbarts
Frequent Advisor

Re: decimals in shell script

Tony,

With printf;

printf "%5.0f" $var

Greetz, Danny
Andreas Voss
Honored Contributor

Re: decimals in shell script

Hi,

just another way:

val=5.6
echo ${val%.*}

Regards
John Palmer
Honored Contributor

Re: decimals in shell script

The Korn or Posix shell's integer arithmetic facility is probably the most elegant solution.

Provided the value supplied is a valid number
(the shell will print a failure message to standard error if it isn't) then

let VAR=
print ${VAR}

will do the trick. The let command implies 'integer VAR' which is an alias to 'typeset -i VAR'

Regards,
John
James R. Ferguson
Acclaimed Contributor

Re: decimals in shell script

Tony:

...and here's yet another:

# V=5.6
# echo "$V / 1"|bc

...JRF...
Curtis Larson
Trusted Contributor

Re: decimals in shell script

if you use a more intelligent shell:

#!/usr/dt/bin/dtksh

var=5.6
print $(( var=int($var) ))

Brian Markus
Valued Contributor

Re: decimals in shell script

Well, Since people were posting some of the alternate ways of doing this. I decided to have some fun. I wrote this one out of pure boredom.


#!/bin/sh
#----------------------------------------------------------
# Script Written by: Brian Markus - bmarkus@rcoe.k12.ca.us
# Reason - pure boredom, and to help a fellow hpux'er
# This script will take any size decimal
# number assign the left side of the
# decimal to a variable named predec,
# then assign the right side of the decimal
# to a variable named postdec
#----------------------------------------------------------
# Set your variable with the decimal
# assign the the decimal number to decnum
decnum=549890.398

#display your variable before altering it
echo "pre number = $decnum"

#get the length of the variable
varlen=`expr length $decnum`

#find the position that the decimal is in
decpos=`expr index $decnum \.`

#set the end point for the left side of the decimal
let decpos=decpos-1

#assign the left side of the decimal to predec
predec=`expr substr $decnum 1 $decpos`

#set the beginning point on the right side of the decimal
let decpos=decpos+2

#assign the right side of the decimal to postdec
postdec=`expr substr $decnum $decpos $varlen`

#put the left and right side together with out the decimal
newnumber=${predec}${postdec}

#display the new built number
echo "post number = $newnumber"



hope this helps


Brian.
When a sys-admin say's maybe, they don't mean 'yes'!
Brian Markus
Valued Contributor

Re: decimals in shell script

If you want to truncate the numbers after the decimal, you could just use the $predec var. and not process the rest. I got carried away. It's always best to follow the KISS rule. Everyone's posted a good solution. This method is a bit more advanced and cumbersome, but it's easy to follow.
When a sys-admin say's maybe, they don't mean 'yes'!