Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. digit roundoff

digit roundoff

Scheduled Pinned Locked Moved C#
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8233601
    wrote on last edited by
    #1

    i have a float type of variable i.e float t1=200.36 and t2=300.62. now i want to roundoff these two variables to their nearest degit. I want to show the information like this: t1=200.36 roundoff=-0.36 total=200 t2=300.62 roundoff=+0.38 total=301

    V J P K 4 Replies Last reply
    0
    • M Member 8233601

      i have a float type of variable i.e float t1=200.36 and t2=300.62. now i want to roundoff these two variables to their nearest degit. I want to show the information like this: t1=200.36 roundoff=-0.36 total=200 t2=300.62 roundoff=+0.38 total=301

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      Look here: Floor[^] Ceiling[^] Round[^] Then you can take the difference of the original with the rounded value. Hope this helps.

      V.

      1 Reply Last reply
      0
      • M Member 8233601

        i have a float type of variable i.e float t1=200.36 and t2=300.62. now i want to roundoff these two variables to their nearest degit. I want to show the information like this: t1=200.36 roundoff=-0.36 total=200 t2=300.62 roundoff=+0.38 total=301

        J Offline
        J Offline
        JF2015
        wrote on last edited by
        #3

        See this code to get the idea:

        double t1 = 200.36;
        double t2 = 300.62;
        int t11 = (int)Math.Round(t1, 0);
        int t21 = (int)Math.Round(t2, 0);
        MessageBox.Show("t1=" + t1.ToString() + " roundoff=" + (t11 - t1).ToString("0.00") + " total=" + Math.Round(t1, 0).ToString());
        MessageBox.Show("t2=" + t2.ToString() + " roundoff=" + (t21 - t2).ToString("0.00") + " total=" + Math.Round(t2, 0).ToString());

        1 Reply Last reply
        0
        • M Member 8233601

          i have a float type of variable i.e float t1=200.36 and t2=300.62. now i want to roundoff these two variables to their nearest degit. I want to show the information like this: t1=200.36 roundoff=-0.36 total=200 t2=300.62 roundoff=+0.38 total=301

          P Offline
          P Offline
          ProEnggSoft
          wrote on last edited by
          #4

          I want to add the following What is required when the number is half i.e. 200.5. Whether 200 or 201? By default Math.Round follows MidpointRounding.ToEven enumeration value.

          double number = 200.5;
          Console.WriteLine (Math.Round(number,0));
          Console.WriteLine (Math.Round(number,0, MidpointRounding.AwayFromZero));
          double number2 = 201.5;
          Console.WriteLine (Math.Round(number2,0));
          Console.WriteLine (Math.Round(number2,0, MidpointRounding.AwayFromZero));
          //The output from the above code will be
          //200
          //201
          //202
          //202

          So, if the next higher number is required always, when the value is half way, then MidpointRounding.AwayFromZero is to be used.

          1 Reply Last reply
          0
          • M Member 8233601

            i have a float type of variable i.e float t1=200.36 and t2=300.62. now i want to roundoff these two variables to their nearest degit. I want to show the information like this: t1=200.36 roundoff=-0.36 total=200 t2=300.62 roundoff=+0.38 total=301

            K Offline
            K Offline
            KUNWAR999
            wrote on last edited by
            #5

            Try this code.. : double g = [any no];//should be of double double L = Math.Floor(g); if (g-L<0.5) { k = (int)(L); } else { k = (int)(L + 1); } now k have ur nearest integer.. :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups