floating point number problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting floating point number problem
# 1  
Old 05-13-2010
Bug floating point number problem

Hello folks

I Hope everyone is fine. I am calculating number of bytes calculation from apache web log.

awk '{ sum += $10 } END { print sum }' /var/httpd/log/mydomain.log
7.45557e+09


it show above number, what should i do it sow number like 7455, i mean if after decimal point above 5 it should 7456, otherwise ignore after decimal and show 7455. please help me out.
# 2  
Old 05-13-2010
I am not quite sure what you are actually asking but I hazard a guess that it involves rounding. The following example shows you how to handle rounding using printf.
Code:
echo "4.444 4.445 4.446" | awk '{printf "%.2f %.2f %.2f\n", $1, $2, $3}'
4.44 4.45 4.45

This works with any number of decimals places in the format string, e.g.
Code:
echo "4.4 4.5 4.6" | awk '{printf "%.0f %.0f %.0f\n", $1, $2, $3}'
4 4 5

# 3  
Old 05-14-2010
thanks, can you help me please, i have my apache log file, and its 10th column have numbers like

10
233
33
333
232


This is basically transferred bytes, how i can i add all lines transfer of byes.
# 4  
Old 05-14-2010
This simple example should help you figure out what you need to do
Code:
$ cat log
10
233
33
333
232
$ awk '{ SUM += $1 } END { print SUM }' log
841
$

# 5  
Old 05-14-2010
But if the values are big and output comes like that -> 7.55902e+09

How to convert this into 744........ (whatever will be the number)

and i want to convert above output in MB after that. Please help me out.
# 6  
Old 05-14-2010
Hi.

Use printf instead of print:
Code:
$ cat file1
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009
100000009

$ awk '{ SUM += $1 } END { print SUM * 1 }' file1
2.2e+09

$ awk '{ SUM += $1 } END { printf "%d\n", SUM }' file1
2200000198

$ awk '{ SUM += $1 } END { printf "%.2f MB\n", SUM / 1024^2 }' file1
2098.08 MB


Last edited by Scott; 05-14-2010 at 08:34 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

3. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

4. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

5. Shell Programming and Scripting

problem with floating point number loops

Hey, I guess I am just to stupid and am not seeing the "wood for the trees", but I am always getting strange errors. I want to create a mesh with coordinates like: x y z 3.1 3.0 0.75 0 0 1 3.1 2.9 0.75 0 0 1 3.1 2.8 0.75 0 0 1 3.1 2.7 0.75 0 0 1 3.0 ... (10 Replies)
Discussion started by: ergy1983
10 Replies

6. Shell Programming and Scripting

using bc with floating point number in files

Hi, I' using bash and I would like to use "bc" to compute the ratio of of two numbers and assign the ratio to a variable. The numbers are in a file, e.g. 196.304492 615.348986 Any idea how to do it? N.B. I cannot change the file to have 196.304492 / 615.348986 as the file is produced by... (14 Replies)
Discussion started by: f_o_555
14 Replies

7. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

8. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies

9. Shell Programming and Scripting

floating point addition

hi, :) I have a file like this 10.456 123.567 456.876 234.987 ........ ....... What i want to do is ia have to add all those numbers and put the result in some other file. Any help pls. cheers RRK (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question