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. Should 0.5 round up or down?

Should 0.5 round up or down?

Scheduled Pinned Locked Moved C#
question
21 Posts 7 Posters 396 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.
  • L Lost User
         float n = 0.2f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
         
         n = 0.5f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 0.6f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.5f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.2f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.7f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    

    Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

    "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

    B Offline
    B Offline
    Bosse62
    wrote on last edited by
    #21

    I learned in school: "When you have a 5 to round from; the number to round to shall be even." Let me give you some examples: Rounding to no decimals: 0.5 --> 0 1.5 --> 2 2.5 --> 2 3.5 --> 4 Rounding to one decimal: 0.35 --> 0.4 0.45 --> 0.4 0.55 --> 0.6 0.65 --> 0.6

    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