Rounding off to nearest number
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file with data like this
vserver-1 vserver-1_root 0.95 0.0019043 0.948047
vserver-1 home 10.00 8.25 1.75
vserver-1 usr 95 45.65 39.35
vserver-1 file0 100 89.15 10.85
Desired formatted output with awk
(rounding off to nearest whole number)
vserver-1 vserver-1_root 1 0 1
vserver-1 home 10 8 2
vserver-1 usr 95 46 39
vserver-1 file0 100 89 11
text-processing awk floating-point
add a comment |Â
up vote
1
down vote
favorite
I have a file with data like this
vserver-1 vserver-1_root 0.95 0.0019043 0.948047
vserver-1 home 10.00 8.25 1.75
vserver-1 usr 95 45.65 39.35
vserver-1 file0 100 89.15 10.85
Desired formatted output with awk
(rounding off to nearest whole number)
vserver-1 vserver-1_root 1 0 1
vserver-1 home 10 8 2
vserver-1 usr 95 46 39
vserver-1 file0 100 89 11
text-processing awk floating-point
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file with data like this
vserver-1 vserver-1_root 0.95 0.0019043 0.948047
vserver-1 home 10.00 8.25 1.75
vserver-1 usr 95 45.65 39.35
vserver-1 file0 100 89.15 10.85
Desired formatted output with awk
(rounding off to nearest whole number)
vserver-1 vserver-1_root 1 0 1
vserver-1 home 10 8 2
vserver-1 usr 95 46 39
vserver-1 file0 100 89 11
text-processing awk floating-point
I have a file with data like this
vserver-1 vserver-1_root 0.95 0.0019043 0.948047
vserver-1 home 10.00 8.25 1.75
vserver-1 usr 95 45.65 39.35
vserver-1 file0 100 89.15 10.85
Desired formatted output with awk
(rounding off to nearest whole number)
vserver-1 vserver-1_root 1 0 1
vserver-1 home 10 8 2
vserver-1 usr 95 46 39
vserver-1 file0 100 89 11
text-processing awk floating-point
edited May 17 at 7:16
SivaPrasath
4,74212445
4,74212445
asked May 17 at 6:29
user1400953
132
132
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
using %0.f
is simplest way to convert float value to nearest whole number:
awk 'printf ("%s %s %.0f %.0f %.0fn",$1,$2,$3,$4,$5)' file
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both1.5
and2.5
round to2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
add a comment |Â
up vote
2
down vote
Assuming you have fixed 5 columns file, then you would do:
awk 'printf("%s %s %d %d %dn",$1, $2, $3+.5, $4+.5, $5+.5)' infile
This adds 0.5 to the fields then %d
will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.
Here, too,printf("%d", x - .5)
doesn't roundx
down correctly:%d
truncates, so ifx=1.3
,x-.5=.8
, then the truncated value is0
.
â ilkkachu
May 17 at 7:03
as a floor function, it was broken.1.3
should floor to1
, not to0
. Again, the point is that the float to int conversion done by%d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the+ 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just useprintf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
add a comment |Â
up vote
0
down vote
to round up , use +0.5 and print via %d
echo "$number" | awk ' printf("%d", $1 + 0.5) '
For your given string ,
vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85
use this command :
awk 'printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %dn" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5' filename
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down withprintf("%d", x - 0.5)
. Ifx
is say,1.3
, thenx - 0.5 = 0.8
, which is truncated to0
. And with+ 0.5
you don't get rounding up, but the usual rounding to nearest with.5
going up.
â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
using %0.f
is simplest way to convert float value to nearest whole number:
awk 'printf ("%s %s %.0f %.0f %.0fn",$1,$2,$3,$4,$5)' file
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both1.5
and2.5
round to2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
add a comment |Â
up vote
2
down vote
accepted
using %0.f
is simplest way to convert float value to nearest whole number:
awk 'printf ("%s %s %.0f %.0f %.0fn",$1,$2,$3,$4,$5)' file
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both1.5
and2.5
round to2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
using %0.f
is simplest way to convert float value to nearest whole number:
awk 'printf ("%s %s %.0f %.0f %.0fn",$1,$2,$3,$4,$5)' file
using %0.f
is simplest way to convert float value to nearest whole number:
awk 'printf ("%s %s %.0f %.0f %.0fn",$1,$2,$3,$4,$5)' file
edited May 17 at 7:05
answered May 17 at 6:59
SivaPrasath
4,74212445
4,74212445
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both1.5
and2.5
round to2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
add a comment |Â
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both1.5
and2.5
round to2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.
â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both
1.5
and 2.5
round to 2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.â ilkkachu
May 17 at 7:07
Also note the comments about floating point rounding in e.g. GNU awk's manual. For example, on my machine both
1.5
and 2.5
round to 2
, since the FP system rounds halves to the nearest even number, not up. This may or may not be what one wants.â ilkkachu
May 17 at 7:07
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
How do I print comma between the values using printf?
â user1400953
May 18 at 2:01
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
comma between only values, or replace all spaces with a comma.
â SivaPrasath
May 18 at 5:38
add a comment |Â
up vote
2
down vote
Assuming you have fixed 5 columns file, then you would do:
awk 'printf("%s %s %d %d %dn",$1, $2, $3+.5, $4+.5, $5+.5)' infile
This adds 0.5 to the fields then %d
will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.
Here, too,printf("%d", x - .5)
doesn't roundx
down correctly:%d
truncates, so ifx=1.3
,x-.5=.8
, then the truncated value is0
.
â ilkkachu
May 17 at 7:03
as a floor function, it was broken.1.3
should floor to1
, not to0
. Again, the point is that the float to int conversion done by%d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the+ 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just useprintf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
add a comment |Â
up vote
2
down vote
Assuming you have fixed 5 columns file, then you would do:
awk 'printf("%s %s %d %d %dn",$1, $2, $3+.5, $4+.5, $5+.5)' infile
This adds 0.5 to the fields then %d
will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.
Here, too,printf("%d", x - .5)
doesn't roundx
down correctly:%d
truncates, so ifx=1.3
,x-.5=.8
, then the truncated value is0
.
â ilkkachu
May 17 at 7:03
as a floor function, it was broken.1.3
should floor to1
, not to0
. Again, the point is that the float to int conversion done by%d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the+ 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just useprintf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Assuming you have fixed 5 columns file, then you would do:
awk 'printf("%s %s %d %d %dn",$1, $2, $3+.5, $4+.5, $5+.5)' infile
This adds 0.5 to the fields then %d
will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.
Assuming you have fixed 5 columns file, then you would do:
awk 'printf("%s %s %d %d %dn",$1, $2, $3+.5, $4+.5, $5+.5)' infile
This adds 0.5 to the fields then %d
will remove the fractional part, resulting in the usual rounding to the nearest integer, with halves (e.g. 2.5) rounded up.
edited May 17 at 7:29
ilkkachu
48.1k669133
48.1k669133
answered May 17 at 6:37
ñÃÂsýù÷
14.7k82361
14.7k82361
Here, too,printf("%d", x - .5)
doesn't roundx
down correctly:%d
truncates, so ifx=1.3
,x-.5=.8
, then the truncated value is0
.
â ilkkachu
May 17 at 7:03
as a floor function, it was broken.1.3
should floor to1
, not to0
. Again, the point is that the float to int conversion done by%d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the+ 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just useprintf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
add a comment |Â
Here, too,printf("%d", x - .5)
doesn't roundx
down correctly:%d
truncates, so ifx=1.3
,x-.5=.8
, then the truncated value is0
.
â ilkkachu
May 17 at 7:03
as a floor function, it was broken.1.3
should floor to1
, not to0
. Again, the point is that the float to int conversion done by%d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the+ 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just useprintf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
Here, too,
printf("%d", x - .5)
doesn't round x
down correctly: %d
truncates, so if x=1.3
, x-.5=.8
, then the truncated value is 0
.â ilkkachu
May 17 at 7:03
Here, too,
printf("%d", x - .5)
doesn't round x
down correctly: %d
truncates, so if x=1.3
, x-.5=.8
, then the truncated value is 0
.â ilkkachu
May 17 at 7:03
as a floor function, it was broken.
1.3
should floor to 1
, not to 0
. Again, the point is that the float to int conversion done by %d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the + 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just use printf("%d", x)
â ilkkachu
May 17 at 7:20
as a floor function, it was broken.
1.3
should floor to 1
, not to 0
. Again, the point is that the float to int conversion done by %d
truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the + 0.5
to begin with. If you want to round towards zero to the nearest integer, it's enough to just use printf("%d", x)
â ilkkachu
May 17 at 7:20
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
Yes, my bad. you are right
â Ã±ÃÂsýù÷
May 17 at 7:21
add a comment |Â
up vote
0
down vote
to round up , use +0.5 and print via %d
echo "$number" | awk ' printf("%d", $1 + 0.5) '
For your given string ,
vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85
use this command :
awk 'printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %dn" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5' filename
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down withprintf("%d", x - 0.5)
. Ifx
is say,1.3
, thenx - 0.5 = 0.8
, which is truncated to0
. And with+ 0.5
you don't get rounding up, but the usual rounding to nearest with.5
going up.
â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
add a comment |Â
up vote
0
down vote
to round up , use +0.5 and print via %d
echo "$number" | awk ' printf("%d", $1 + 0.5) '
For your given string ,
vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85
use this command :
awk 'printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %dn" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5' filename
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down withprintf("%d", x - 0.5)
. Ifx
is say,1.3
, thenx - 0.5 = 0.8
, which is truncated to0
. And with+ 0.5
you don't get rounding up, but the usual rounding to nearest with.5
going up.
â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
add a comment |Â
up vote
0
down vote
up vote
0
down vote
to round up , use +0.5 and print via %d
echo "$number" | awk ' printf("%d", $1 + 0.5) '
For your given string ,
vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85
use this command :
awk 'printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %dn" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5' filename
to round up , use +0.5 and print via %d
echo "$number" | awk ' printf("%d", $1 + 0.5) '
For your given string ,
vserver-1 vserver-1_root 0.95 0.0019043 0.948047 vserver-1 home 10.00 8.25 1.75 vserver-1 usr 95 45.65 39.35 vserver-1 file0 100 89.15 10.85
use this command :
awk 'printf "%s %s %d %d %d %s %s %d %d %d %s %s %d %d %d %s %s %d %d %dn" , $1, $2, $3+0.5, $4+0.5, $5+0.5, $6, $7, $8+0.5, $9+0.5, $10+0.5, $11, $12, $13+0.5, $14+0.5, $15+0.5, $16, $17, $18+0.5, $19+0.5, $20+0.5' filename
edited May 17 at 7:17
answered May 17 at 6:45
mkmayank
36310
36310
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down withprintf("%d", x - 0.5)
. Ifx
is say,1.3
, thenx - 0.5 = 0.8
, which is truncated to0
. And with+ 0.5
you don't get rounding up, but the usual rounding to nearest with.5
going up.
â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
add a comment |Â
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down withprintf("%d", x - 0.5)
. Ifx
is say,1.3
, thenx - 0.5 = 0.8
, which is truncated to0
. And with+ 0.5
you don't get rounding up, but the usual rounding to nearest with.5
going up.
â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
looking at the markdown source of the original question, I think they meant the input data was split on lines, five fields each
â ilkkachu
May 17 at 6:51
Also, you can't round down with
printf("%d", x - 0.5)
. If x
is say, 1.3
, then x - 0.5 = 0.8
, which is truncated to 0
. And with + 0.5
you don't get rounding up, but the usual rounding to nearest with .5
going up.â ilkkachu
May 17 at 7:01
Also, you can't round down with
printf("%d", x - 0.5)
. If x
is say, 1.3
, then x - 0.5 = 0.8
, which is truncated to 0
. And with + 0.5
you don't get rounding up, but the usual rounding to nearest with .5
going up.â ilkkachu
May 17 at 7:01
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
yeah you are right, this hack works only for going up
â mkmayank
May 17 at 7:16
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444292%2frounding-off-to-nearest-number%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password