By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,280 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,280 IT Pros & Developers. It's quick & easy.

math operations

P: 7
Hello Again ,

I have some problems with math operations ...

I have to sum numbers with max 4 decimals , like

12.3456
4.2
15.33
etc. etc.

and then divide them to have an average value . More numbers I have , more decimals I have in the resulting average , like
12.4567890123
How can I determine a fixed number of decimals in the result ?


Thx

David
Jan 8 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hello Again ,

I have some problems with math operations ...

I have to sum numbers with max 4 decimals , like

12.3456
4.2
15.33
etc. etc.

and then divide them to have an average value . More numbers I have , more decimals I have in the resulting average , like
12.4567890123
How can I determine a fixed number of decimals in the result ?


Thx

David
What have you tried thus far? Why not post your code and we will try and assist you from there.

Regards,

Jeff
Jan 8 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
It's a simple question, he can use sprintf:

Expand|Select|Wrap|Line Numbers
  1. $results = 12.4567890123;
  2. $results = sprintf ("%.4f", $results);
  3. print $results;
Jan 8 '08 #3

P: 7
Thx , I'll try sprintf .
Anyway that's my code ...
Expand|Select|Wrap|Line Numbers
  1. sub Average_Value{
  2.     if (-e "$File_Dir/$Sensor_Dir.$Feature.sum.drn"){
  3.                     open (SUM , "<$File_Dir/$Sensor_Dir.$Feature.sum.drn")or die "Can't open $File_Dir/$Sensor_Dir.$Feature.sum.drn!\n ";
  4.                     $Sum=<SUM>;
  5.                     $New_Sum=$Sum+$Last_Value;
  6.                     close (SUM);
  7.                     open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.sum.drn")or die "Can't open Sensor1_temp_sum.drn!\n ";
  8.                     print SUM "$New_Sum";
  9.                     close (SUM);
  10.                 }else{
  11.                 open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.sum.drn");
  12.                 $New_Sum=$Last_Value;
  13.                 print SUM "$New_Sum";
  14.                 close (SUM);
  15.                 }
  16.         $Average=$New_Sum/$Count;
  17.         open (SUM , ">$File_Dir/$Sensor_Dir.$Feature.average.drn");
  18.         print SUM "$Average";
  19.         close (SUM);
  20.     return 1;
  21.     }
  22.  
  23.  

Reading the manual of sprintf there is a thing I can't understand ...
Does it just "cut" the number of decimals or does it approximate to the next closest value ?
I need something like 1.78997 -> 1.7900 , not 1.7899 .


thx again guys
Jan 8 '08 #4

P: 7
I tried with your example , and seems sprint DO approximate correctly the value :)

Thx

D.
Jan 8 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.