Round up the decimals

Hi All,

I would like to do the following in the shell script

561.76 to 562

I tried using this echo 'scale=0; 749 * 75 /100 ' | bc

but just returned only 561

Please help me . I appreciate your help

Thanks
rajeevm

bc is still an integer calculator, it just tracks the decimal point and adjusts later.

You can round up the same way in bc the same way you would in pure integer math. Add 0.5 (times 100) before you divide. Numbers that would round up will bump past nine, numbers that round down won't.

echo $((((749 * 75)+50) /100))

Try like..

echo "561.76" | awk '{printf("%d\n",$0+=$0<0?-0.5:0.5)}' 

---------- Post updated at 02:23 AM ---------- Previous update was at 02:17 AM ----------

One more..

printf "%.0f\n" "561.76" 

try this..

echo "561.76" | awk '{printf "%.0f",$0}'

awk 'BEGIN {printf "%.0f",( 749 * 75 /100) }'

I have tried using this

awk 'BEGIN {printf "%.0f",($TOTAL_RECORDS * 75 /100) }'

but it returned 0 instead of 562

Did I do anything wrong .Please help me out.

Thanks
rajeevm

try this..

awk -v VAR="$TOTAL_RECORDS" 'BEGIN {printf "%.0f",(VAR * 75 /100) }'

$TOTAL_RECORDS this is shell variable not awk..

Thanks Pamu

That code for one but its not working for the second

which is I need to split the number of records into 75% and 25 % so i did this for 75 % just like you mentioned

but i implemented the same for 25 % like below

25%=`awk -v VAR="$TOTAL_RECORDS" 'BEGIN {printf "%.0f",(VAR * 25 /100) }'`

the result is 187 but instead of 188 as the result would be 749*25/100=187.25 in that I would be missing one record when I will these records to a file

I appreciate your help.

Thanks
rajeevm

as a result 187.25 that's why it is rounded up to 187..

try this...

awk -v VAR="$TOTAL_RECORDS" 'BEGIN {s=(VAR * 25 /100);split(s,a,".");if(a[2] > 49 ){printf "%.F",s}else{printf "%.F",(s+0.50} }'

try this

echo | awk '{ print 749 * 75 / 100 } '

And also I would like to write these 75 % and 25 % records to two new files for that I did something like this

File75=`head -n $REC_75 $File > temp1.txt`

File25=`tail -n $REC_25 $File >temp2.txt`

its not writing anything into the files

Please let me know whether I am doing anything wrong. I appreciate it.

Thanks
rajeevm

---------- Post updated at 11:44 AM ---------- Previous update was at 11:15 AM ----------

Thanks again Pamu

but the code you gave for ReC_25 is not working i am getting this error

"unexpected newline or end of string"

awk: cmd. line:1: BEGIN {s=(VAR * 25 /100);split(s,a,".");if(a[2] > 49 ){printf "%.F",s}else{printf "%.F",(s+0.50) }
awk: cmd. line:1:                                                                                                   ^ unexpected newline or end of string

I appreciate your response

Thanks
rajeevm

show us what you are doing ...

#!bin/sh
#Param
INPUT_FILE='abc.txt'

TOTAL_RECORDS=$(wc -l < $INPUT_FILE)

echo $TOTAL_RECORDS

REC_75=`awk -v VAR="$TOTAL_RECORDS" 'BEGIN {printf "%.0f",(VAR * 75 /100) }'`

echo "$REC_75"

REC_25=`awk -v VAR="$TOTAL_RECORDS" 'BEGIN {s=(VAR * 25 /100);split(s,a,".");if(a[2] > 49 ){printf "%.F",s}else{printf "%.F",(s+0.50)}'`

echo "$REC_25"

I am writing these two REC_75 and REC_25 into two different files REC_75 is working fine and REC_25 is not working I got the error which I mentioned in the earlier email.

bracket missing... try now..

Thanks Pamu

It worked.

I appreciate it.

Thanks
rajeevm