Percentage Calculation in Decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Percentage Calculation in Decimal
# 8  
Old 11-30-2012
printf sees the output of bc.So, bump up the scale for bc a bit:
Code:
printf "%.2f\n" $(echo -e "scale=10\n(($b-$a)*100)/$b"|bc)

# 9  
Old 11-30-2012
sorry my bad. I should have checked the actual problem ..

here is the real problem

printf "%.2f\n" 12.355 --> output 12.35

But I need it as 12.36
# 10  
Old 11-30-2012
Seem to be a bug that may not be so simple to solve.

Code:
12.355000 -> 12.35 correct 12.36
12.355001 -> 12.36 correct 12.36

# 11  
Old 11-30-2012
Quote:
Originally Posted by Anupam_Halder
printf "%.2f\n" 12.355 --> output 12.35

But I need it as 12.36

Code:
printf "%.2f\n" $(echo -e "scale=10\n(($b-$a)*100)/$b"+0.01|bc)

It will solve your problem. Smilie
# 12  
Old 11-30-2012
will it? What if I have like 12.991 ?
Then it will make it 13 right? Should I add some more small value? Smilie
Like 0.0001 Smilie
# 13  
Old 11-30-2012
Quote:
Originally Posted by Anupam_Halder
will it? What if I have like 12.991 ?
Then it will make it 13 right? Should I add some more small value? Smilie
Like 0.0001 Smilie
good catch...Smilie

my bad Smilie

if you think about small value then still it will be same case if you find there also 12.9999 for 0.0001. Smilie

It better if you check the digit after .
if it is 55 then change it to 56.

I know this is also not good way of doing it..

try
Code:
printf "%.2f\n" $(echo -e "scale=10\n(($b-$a)*100)/$b"|bc | sed 's/\.55/\.56/')

This User Gave Thanks to pamu For This Post:
# 14  
Old 11-30-2012
one more problem ..

what if the value is 12.55? It will make it 12.56 right?
And also we may need to take care many more things Smilie

Like it's not only that I will get 12.55

I may get 1.155 --> And it should give me 1.16
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk percentage calculation from a file

i have a file say test with the below mentioned details Folder Name Total space Space used /test/test1 500.1GB 112.0 GB /test/test2 3.2 TB 5TB /test/test3 3TB 100GB i need to calculate percentage of each row based on total space and space used and copy... (9 Replies)
Discussion started by: venkitesh
9 Replies

2. Shell Programming and Scripting

Percentage calculation

Hi, I have a text file in below format. I trying to find a solution for finding percentage used for each of the NAMEs. Directory ALLOCATED USED NAME1 93MB 93KB NAME2 25G 62K NAME3 14G 873M NAME4 25G 62K NAME5 20G... (10 Replies)
Discussion started by: ctrld
10 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. Shell Programming and Scripting

Percentage calculation

i am trying to get percentage : but not able to do it: i tried : x=1 y=2 z=`expr $x/$y*100` it is not giving me result can u pls help on this (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies

5. Shell Programming and Scripting

Decimal number calculation problem

I have a below snippet of code from my perl script and its causing a problem when the output of $lTAX is 0 (zero) its getting displayed as -0.00. I want output to be 0 not -0.00. Any help would be appreciated. #!/usr/bin/perl my $lTotA = 50.94; my $lVatA = 8.49; my $lAllocD; my $lAdjNr =... (4 Replies)
Discussion started by: Devesh5683
4 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

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

9. Shell Programming and Scripting

decimal calculation

Hi am calculating the percentage of the pass and fail. Pass: echo `echo 1498*100/1667| bc`% fail: echo `echo 169*100/1667 | bc`% for this am getting PASS= 89% fail =10 % I need to fetch the exact decimal percentage like PASS = 89.8 % FAIL= 10.2 % Please advice me (3 Replies)
Discussion started by: bobprabhu
3 Replies
Login or Register to Ask a Question