Decimal number calculation problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Decimal number calculation problem
# 1  
Old 11-28-2012
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.


Code:
#!/usr/bin/perl
my $lTotA = 50.94;
my $lVatA = 8.49;
my $lAllocD;
my $lAdjNr = 'A23232';
my $lAcctNr = '2000971';
my $TAX = 90018;
my $UNKNOWN = 90020;
my $VAT_EXEMPT     = 9000019;
$lAllocD{$lAcctNr}{$lAdjNr}{$TAX} = 3.7;
$lAllocD{$lAcctNr}{$lAdjNr}{$UNKNOWN} = 0.0;
$lAllocD{$lAcctNr}{$lAdjNr}{$VAT_EXEMPT} = 38.75;



my $lTAX  =   sprintf("%0.2f",$lTotA-$lVatA- $lAllocD{$lAcctNr}{$lAdjNr}{$TAX}- $lAllocD{$lAcctNr}{$lAdjNr}{$UNKNOWN}-$lAllocD{$lAcctNr}{$lAdjNr}{$VAT_EXEMPT});
print "\n**TAXABLE AMT is:$lTAX\n";

# 2  
Old 11-28-2012
Use %d format specifier:-
Code:
my $lTAX  =   sprintf("%d",$lTotA-$lVatA- $lAllocD{$lAcctNr}{$lAdjNr}{$TAX}- $lAllocD{$lAcctNr}{$lAdjNr}{$UNKNOWN}-$lAllocD{$lAcctNr}{$lAdjNr}{$VAT_EXEMPT});

# 3  
Old 11-28-2012
If you're calculating financial numbers, it may be a good idea to calculate with integers. Count in cents or tenths of a cent with no decimal points, then divide by 10 or 100 to get the dollar result. You won't end up with strange things like negative zero.
# 4  
Old 11-28-2012
Thanks bipinajith, Corona688 for the reply.
The code I pasted is collecting data and processing millions of records, its displaying negative zero in very few cases where result of the formula is zero. I want result to be displayed up to 2 decimal places, hence using %d is not an option as suggested. I am trying to understand suggestion by Corona, if you can please give me an example so that I can implement same in the code.
# 5  
Old 12-04-2012
It wouldn't be printing a minus sign if the results of the formula was zero. It's a negative number, but smaller than the number of decimal points you're printing.

I don't know what sort of example I can show you. Calcualting in cents means just that.

Code:
my $cents=100;

# ... bunch of calculations

$cents=int($cents); # Make sure no leftover fraction

printf("%.2f dollars\n", $cents/100);

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Percentage Calculation in Decimal

Hi, I have two variable and I need to calculate the percentage of them. Example: (b-a)*100/b How can I do it? I need to do it till 2 decimal point. (16 Replies)
Discussion started by: Anupam_Halder
16 Replies

2. Shell Programming and Scripting

Matching a decimal number?

Hi everyone! Easy question for everyone. I'm trying to run a command line to find an exact match of a decimal number within a file. The number can be a positive OR negative number. For instance, if I want to find only the number -1 in the file that has: -17.6 -17 -16.3 -16.2 -15.7 -15.3... (6 Replies)
Discussion started by: lucshi09
6 Replies

3. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

4. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

5. Shell Programming and Scripting

Add zero in decimal number

echo "scale=2; 282.73/640" | bc This will print .44 How to make the variable as 0.44 (2 Replies)
Discussion started by: sandy1028
2 Replies

6. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

7. Shell Programming and Scripting

Test decimal number

Hi, I would like test if a number is a decimal number or not (9 Replies)
Discussion started by: francis_tom
9 Replies

8. 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

9. Shell Programming and Scripting

need help converting string to number for calculation

I've searched the forum and google, but can't see an answer to this simple problem. Here's my small test script: #!/bin/csh echo "enter a number:" read num echo "you entered $num" set num = `expr $num + 1` echo new value is $num can someone show me how to do this calculation? note that... (4 Replies)
Discussion started by: tpatput
4 Replies
Login or Register to Ask a Question