454,280 Members | 1,693 Online 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
4 Replies

 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

 Expert 2.5K+ P: 4,059 It's a simple question, he can use sprintf: Expand|Select|Wrap|Line Numbers \$results = 12.4567890123; \$results = sprintf ("%.4f", \$results); print \$results; Jan 8 '08 #3

 P: 7 Thx , I'll try sprintf . Anyway that's my code ... Expand|Select|Wrap|Line Numbers sub Average_Value{     if (-e "\$File_Dir/\$Sensor_Dir.\$Feature.sum.drn"){                     open (SUM , "<\$File_Dir/\$Sensor_Dir.\$Feature.sum.drn")or die "Can't open \$File_Dir/\$Sensor_Dir.\$Feature.sum.drn!\n ";                     \$Sum=;                     \$New_Sum=\$Sum+\$Last_Value;                     close (SUM);                     open (SUM , ">\$File_Dir/\$Sensor_Dir.\$Feature.sum.drn")or die "Can't open Sensor1_temp_sum.drn!\n ";                     print SUM "\$New_Sum";                     close (SUM);                 }else{                 open (SUM , ">\$File_Dir/\$Sensor_Dir.\$Feature.sum.drn");                 \$New_Sum=\$Last_Value;                 print SUM "\$New_Sum";                 close (SUM);                 }         \$Average=\$New_Sum/\$Count;         open (SUM , ">\$File_Dir/\$Sensor_Dir.\$Feature.average.drn");         print SUM "\$Average";         close (SUM);     return 1;     }     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 