Rounding numbers

jojo

New Member
Credits
38
Hello Guys, I'm terribly new to linux flavor and starting to learn bash scripting. Can someone please help me how to round off this number in column 3 ($3). I do have a file called resources and contents are the dates. Please see below.

2021-06-24 19:20:00, 4.946197
2021-06-24 19:25:00, 6.960327
2021-06-24 19:30:00, 2.088151
2021-06-24 19:30:00, 2.092754
2021-06-24 19:30:00, 5.019269
2021-06-24 19:30:00, 7.028185
2021-06-24 19:35:00, 6.910371
2021-06-24 19:40:00, 7.000000
2021-06-24 19:45:00, 0.127442
2021-06-24 19:50:00, 6.878528

Thanks.
 


JasKinasis

Well-Known Member
Credits
7,561
Bash can only handle integer mathematics.
To deal with calculating/manipulating real/decimal numbers you'd need to use something like bc, or awk, or a scripting language like perl or python.

Personally, I think using awk will probably be best.
But do you want the floating point numbers to be rounded to a certain number of decimal places? or to the nearest, whole integer?

To round to two decimal places, you could use awks printf command like this:
Bash:
awk '{printf "%s %s %.2f\n",$1,$2,$3}' /path/to/file
Where /path/to/file is the path to your input file.
That will print each line in your file and will round the value in the third column to two decimal places.

The key part of the above command is the format specifier for awk's printf command:
Code:
printf "%s %s %.2f\n",$1,$2,$3
The format specifier for awk's printf is the part between the double quotes.
The two instances of %s specifies that we're printing two strings in our output.
%.2f specifies that we're printing a decimal number - rounded to two decimal places.
And the \n at the end specifies that we'll be printing a newline at the end of each line.
And after the closing double quote, we have the list of fields we'll be printing. As per the format-specifier - $1 and $2 will be printed as strings, $3 will be printed as a decimal number rounded to two decimal places.

If you want to round the third column to the nearest, whole integer - simply use %.0f (round to zero decimal places) in the format specifier for the third field in awks printf, like this:
Bash:
awk '{printf "%s %s %.0f\n",$1,$2,$3}' /path/to/file
Once again, where /path/to/file is the path to your input file.
Above will print all three fields from each record in the file, but the third field will be rounded to the nearest, whole integer.

Likewise, if you want to round the number to three or four decimal places, you can modify the number in the format specifier for the third field.

And if you want to write the results out to a new file, you can do that via output redirection.
e.g.
To round the third column of each line to three decimal places and to write the results to a new file, we'd do this:
Bash:
awk '{printf "%s %s %.3f\n",$1,$2,$3}' /path/to/file > /path/to/outputFile
Where /path/to/file is the path to your input file and /path/to/outputFile is the path to save a new file to.

I hope this helps!
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top