deciaml places


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers deciaml places
# 1  
Old 08-24-2007
deciaml places

Hello All,

Can anyone please tell me how do i define the variable as decimal.
Below is my script. After division i get a whole number where as i want to see the decimal places also..

TABLEAVG=`expr ${TABLECHARS}/${TABLELINES} | bc`

EG: if TABLECHARS= 1280 and TABLELINES=44 the
O/P is 29.0909

I want the decimal to be diplayed on the screen..whereas what i get now is 29

I want the output with the decimal places to be displayed
# 2  
Old 08-24-2007
Code:
TABLEAVG=`echo " $TABLECHARS/$TABLELINES" | bc -l`
printf "%5.4f" $TABLEAVG

printf -- the %5.4 controls the number of decimal places - otherwise you can a get a lot whith just
Code:
echo $TABLEAVG

# 3  
Old 08-24-2007
Quote:
Originally Posted by jim mcnamara
Code:
TABLEAVG=`echo " $TABLECHARS/$TABLELINES" | bc -l`
printf "%5.4f" $TABLEAVG

printf -- the %5.4 controls the number of decimal places - otherwise you can a get a lot whith just
Code:
echo $TABLEAVG

or you can define your own 'scale' for 'bc':
Code:
TABLEAVG=`echo "scale=2; $TABLECHARS/$TABLELINES" | bc -l`

# 4  
Old 08-25-2007
With zsh:

Code:
% ((TABLEAVG=1280./44));print $TABLEAVG
29.0909090909

If you want to specify how many significant digits you want in the output (4 in the example below):

Code:
% declare -F4 TABLEAVG='1280./44';echo $TABLEAVG
29.0909

With ksh93(or dtksh on Solaris, for example):

Code:
$ typeset -F4 TABLEAVG='1280./44';echo $TABLEAVG
29.0909

# 5  
Old 08-27-2007
Quote:
Originally Posted by vgersh99
or you can define your own 'scale' for 'bc':
Code:
TABLEAVG=`echo "scale=2; $TABLECHARS/$TABLELINES" | bc -l`

Thanku so much...it works...

But i have a problem...
I have given an if statment stating that if its whole number output the "TABLEAVG"
else
value with decimal...

if [ $TABLEAVG == 29 ]
then
echo "The file is good with count of comma per line : $TABLEAVG"
else
echo "The file is corrupt with commas : $TABLEAVG"
fi

If the output is 29, it displays 29.00 because i have given the sacle..is there any workaround for this..

thanks
# 6  
Old 08-27-2007
I guess I don't understand what it is that you really want: you want an integer representation OR you want float with a 'scale'?
# 7  
Old 08-27-2007
Quote:
Originally Posted by vgersh99
I guess I don't understand what it is that you really want: you want an integer representation OR you want float with a 'scale'?

I need both..
Eg: as per the script if the tablechar=58 and the tablelines=2 the output after division is 29(tableavg)
now, in the IF statment

if [ $TABLEAVG == 29 ]
then
echo "The file is good with count of comma per line without any decimal point : $TABLEAVG"
else
echo "The file is corrupt with commas having decimal values : $TABLEAVG"
fi

i dont want the decimal places if the is it 29.000(the zeros have to be eliminated), where as if the o/p is 29.09 i want that ouput with decimal values..

thanks in advance
and say for eg: if the output is with
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Programming

Badly places ()'s on C

I dont know why this Linux would give me badly placed () error all the time for this; #include <stdio.h> int main() { register int num=0 ; while ((num < 5)) ++num; printf("Pass %d \n", num) ; return 0 ; } can anyone help me please? (11 Replies)
Discussion started by: sizzler786
11 Replies

3. Solaris

Sync script between two places

Hello Team, We would like to copy or sync a couple a zpool and all dataset inside between two datacenters one time per day, the structure and space are the same in both sides. We are consider to use rsync and a cron job. We would like to know what type of ports we have to open in the... (1 Reply)
Discussion started by: csierra
1 Replies

4. UNIX for Advanced & Expert Users

How to interchange the places of 1st name and last name?

cat emp.lst 12 |Rob Cliff |G.M. 14 |Mark Rob |Chairman Please use awk to invert the names in the file emp.lst, i.e. the surname should be 1st and then the 1st name. There are trailing spaces even in each field making them fixed length. (4 Replies)
Discussion started by: ravisingh
4 Replies

5. Shell Programming and Scripting

Channeling output to 2 places

Below is my requirement. When I type 'ls' from the command prompt, 1. I need the output/listing to be shown on screen(which happens by default) 2. Also a file named a.txt should be populated with the output of 'ls' comma nd. How should I do this? -Yuvaraj. (2 Replies)
Discussion started by: Yuvaraj737
2 Replies

6. Shell Programming and Scripting

Insert underscore at certain places

Hi all, I have to insert underscore at certain places(places before and after PAxxx/PAxxxx entries in a big file like this ESR1 PA156 leflunomide PA450192 CHST3 PA26503 docetaxel tungstate Pa4586; thalidomide Pa34958; PAxxx/PAxxxx entries are metioned between 2 names in each row ... (4 Replies)
Discussion started by: manigrover
4 Replies

7. Shell Programming and Scripting

Decimal places

i need to multiplay a number with 1.00.. so that the output should contain two decimal places at end.. for example... 236 * 1.00 = 236.00 245.8 * 1.00 = 245.80 but when i perform multiplication it shows output as. 236 245.8 can anyone help me to get the actual output of... (11 Replies)
Discussion started by: arunmanas
11 Replies

8. Shell Programming and Scripting

Four decimal places with awk

i have a script in which awk prints "($2-1700)/10000" and the answer is -0.07,but i want the answer in 4 decimal places. that is -0.0700. How can i sue awk to get my results in four decimal places (4 Replies)
Discussion started by: tomjones
4 Replies

9. Shell Programming and Scripting

Rotate an array by 2 places

I have an array a={0,1,2,3,4} The out put should be: 3,4,0,1,2 Please can any body write code for this program? Thanks! (4 Replies)
Discussion started by: ansar basha.k
4 Replies

10. UNIX for Advanced & Expert Users

HP-UX lp spooling places

Does anybody know if HP-UX uses any other directories besides /var/spool/lp/request to spool print jobs? I checked the man pages, and it only mentions /var/spool/lp in general. I was just wondering if anyone knew of some other place it might possibly use. thanks in advance for any help! (2 Replies)
Discussion started by: doeboy
2 Replies
Login or Register to Ask a Question