Rounding off to nearest number

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question

























    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






    share|improve this question























      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






      share|improve this question













      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








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 17 at 7:16









      SivaPrasath

      4,74212445




      4,74212445









      asked May 17 at 6:29









      user1400953

      132




      132




















          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





          share|improve this answer























          • 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










          • comma between only values, or replace all spaces with a comma.
            – SivaPrasath
            May 18 at 5:38

















          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.






          share|improve this answer























          • 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










          • Yes, my bad. you are right
            – Î±Ò“sнιη
            May 17 at 7:21

















          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





          share|improve this answer























          • 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










          • yeah you are right, this hack works only for going up
            – mkmayank
            May 17 at 7:16










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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





          share|improve this answer























          • 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










          • comma between only values, or replace all spaces with a comma.
            – SivaPrasath
            May 18 at 5:38














          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





          share|improve this answer























          • 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










          • comma between only values, or replace all spaces with a comma.
            – SivaPrasath
            May 18 at 5:38












          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





          share|improve this answer















          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






          share|improve this answer















          share|improve this answer



          share|improve this answer








          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 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










          • 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










          • 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












          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.






          share|improve this answer























          • 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










          • Yes, my bad. you are right
            – Î±Ò“sнιη
            May 17 at 7:21














          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.






          share|improve this answer























          • 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










          • Yes, my bad. you are right
            – Î±Ò“sнιη
            May 17 at 7:21












          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.






          share|improve this answer















          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.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          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 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










          • 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











          • 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















          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










          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





          share|improve this answer























          • 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










          • yeah you are right, this hack works only for going up
            – mkmayank
            May 17 at 7:16














          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





          share|improve this answer























          • 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










          • yeah you are right, this hack works only for going up
            – mkmayank
            May 17 at 7:16












          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





          share|improve this answer















          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






          share|improve this answer















          share|improve this answer



          share|improve this answer








          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 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
















          • 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










          • 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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay