addition of decimal no


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting addition of decimal no
# 1  
Old 09-07-2011
Error addition of decimal no

Code:
a=10.00
pattern=-11.00
b=`echo "$a $pattern" | awk ' printf("%d\n", $1 + $2)'`
echo $b

not working, also trined bc ,dc but thats not on my m/c.
also expr not supporting.

any clue?

Last edited by Scott; 09-07-2011 at 07:25 AM.. Reason: Code tags, please...
# 2  
Old 09-07-2011
Code:
 echo "$a $pattern" | awk '{a=$1+$2; print a}'

--ahamed

Use nawk if in Solaris

PS : Check the solution/format/usage provided in the previous post regarding multiplication.
https://www.unix.com/shell-programming-scripting/166718-error-multiplying-negative-decimal-values.html
# 3  
Old 09-07-2011
corrected one ..
Code:
$ echo "10.00 -11.00" | awk ' {printf("%d\n", $1 + $2)}'
-1

# 4  
Old 09-07-2011
Quote:
Originally Posted by jayan_jay
corrected one ..
Code:
$ echo "10.00 -11.00" | awk ' {printf("%d\n", $1 + $2)}'
-1

it avoids the decimal value.
I tried this but answer is not correct.
Code:
a=10.11
pattern=-11.01
b=`echo "$a $pattern" | awk  {' printf("%d\n", $1 + $2)'}`
echo $b

Decimal + negative value additionSmilie

Last edited by radoulov; 09-07-2011 at 07:25 AM.. Reason: Code tags.
# 5  
Old 09-07-2011
Code:
/user/ahamed/login $ echo "10.11 -11.00" | awk '{a=$1+$2; print a}'
-0.89

/user/ahamed/login $ echo "10.11 -11.00" | nawk '{ printf("%f\n", $1 + $2)}'
-0.890000

--ahamed
# 6  
Old 09-07-2011
Quote:
Originally Posted by ahamed101
Code:
 echo "$a $pattern" | awk '{a=$1+$2; print a}'

--ahamed

Use nawk if in Solaris

PS : Check the solution/format/usage provided in the previous post regarding multiplication.
https://www.unix.com/shell-programming-scripting/166718-error-multiplying-negative-decimal-values.html
I tried that but the format was exponatial while doing addition

---------- Post updated at 05:25 AM ---------- Previous update was at 05:18 AM ----------

Quote:
Originally Posted by ahamed101
Code:
/user/ahamed/login $ echo "10.11 -11.00" | awk '{a=$1+$2; print a}'
-0.89

/user/ahamed/login $ echo "10.11 -11.00" | nawk '{ printf("%f\n", $1 + $2)}'
-0.890000

--ahamed
i need the o/p only in 2 decimal value like 0.89
# 7  
Old 09-07-2011
Code:
/user/ahamed/login $ echo "10.11 -11.00" | nawk '{ printf("%0.2f\n", $1 + $2)}'
-0.89

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. Shell Programming and Scripting

Decimal Padding in Decimal

Hi Experts, I have requirement to pad a decimal number that should have fixed length as 10. if number is 234.234 > 234.234000 if number is 12.4 > 12.4000000 if number is 3456.5678 > 3456.56780 from above example we can see that overall length is 10 and padding is being done right sided of... (2 Replies)
Discussion started by: looney
2 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

5. Shell Programming and Scripting

Bash Decimal Addition using bc

Hi guys/gals i have a quick question. I am trying to read from a file and add the values up but the problem is the values are not integers their floats so i tried to used bc but failed epicly lol. Any tips would be great. Thanks this is the code i have so far : while read num do ... (6 Replies)
Discussion started by: vb615
6 Replies

6. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

7. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

8. Shell Programming and Scripting

Help with addition

Hi all, I am getting following output by using commands like sort, uniq and awk to the standard output. 110 d 40 a 59 c 9 b 3 e Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3). Also the output may... (3 Replies)
Discussion started by: tenderfoot
3 Replies

9. Shell Programming and Scripting

incremental addition of hex decimal number in one field

Hi I want to incremental add hex decimal number to a particula field in file eg: addr =123 dept1=0 addr = 345 dept2 =1 addr2 = 124 dept3 =2 . . . . . . addr3 =567 dept15 =f Is there any command which add... (8 Replies)
Discussion started by: diddi_linux
8 Replies
Login or Register to Ask a Question