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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Rounding numbers?

Rounding numbers?

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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.
  • G Offline
    G Offline
    Gavin Roberts
    wrote on last edited by
    #1

    Hi all, I was wondering if there is anyway that you could use Math.Round or some other method/class to round numbers off. Basically if I have the number 19 or 256 etc I would like to round it off to the nearest ten, ie 19 rounded to 20 256 rounded to 260 I've tried using Math.Round but that only works for decimals. Any idea's? TIA Gav

    S M 2 Replies Last reply
    0
    • G Gavin Roberts

      Hi all, I was wondering if there is anyway that you could use Math.Round or some other method/class to round numbers off. Basically if I have the number 19 or 256 etc I would like to round it off to the nearest ten, ie 19 rounded to 20 256 rounded to 260 I've tried using Math.Round but that only works for decimals. Any idea's? TIA Gav

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      The following should work:

      double x = 256;
      x = Math.Round(x / 10.0) * 10.0;


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • G Gavin Roberts

        Hi all, I was wondering if there is anyway that you could use Math.Round or some other method/class to round numbers off. Basically if I have the number 19 or 256 etc I would like to round it off to the nearest ten, ie 19 rounded to 20 256 rounded to 260 I've tried using Math.Round but that only works for decimals. Any idea's? TIA Gav

        M Offline
        M Offline
        mikone
        wrote on last edited by
        #3

        i dont know if there already is a function. if not try this: function roundup(int myval) { while (myval % 10 != 0) myval++; return myval; } usage: roundup(15); output: 20 if you want it to use the "real" rules, you will have to do it like this: function round(int myval) { int runs = 0; while ((myval + runs) % 10 != 0) { runs++; } if (runs < 5) return (myval - 10); return myval; } EDIT: the post before this one shows another solution which is much simpler and also looks better (since it's using inbuilt technology it probable will be faster too :))

        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