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. Database & SysAdmin
  3. Database
  4. The Math.Round() in VS.NET and Round() in SQL Server 2000

The Math.Round() in VS.NET and Round() in SQL Server 2000

Scheduled Pinned Locked Moved Database
questioncsharpdatabasesql-servervisual-studio
2 Posts 2 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.
  • B Offline
    B Offline
    Blue_Skye
    wrote on last edited by
    #1

    I have a decimal value 23.25 . When i use the math.round(23.25,1) in VS.NET, the result is 23.2 but with the function: round(23.25,1) in SQL Server 2000, the result is 23.3 :confused: How can i let them return the same value ? Thanks!

    R 1 Reply Last reply
    0
    • B Blue_Skye

      I have a decimal value 23.25 . When i use the math.round(23.25,1) in VS.NET, the result is 23.2 but with the function: round(23.25,1) in SQL Server 2000, the result is 23.3 :confused: How can i let them return the same value ? Thanks!

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      SQL and .Net use different rounding algorithms (as you have observed), ao the answer is that you can't. Net's version is the more 'proper' algorithm: it returns the nearest even number when the value is half way between (.25 will round to .2, .35 will round to .4) This yeilds more accurate averages and totals for the rounded data, and so is prefered. It is sometimes called "Banker's rounding". SQL always rounds to the next larger number for the mid value, which leads to totals and averages that are biased to the high side. I would recommend choosing the rounding behavior that you prefer, and always doing the rounding that way (i.e. either have ONLY SQL do the rounding, or ONLY .Net). I suspect that returning unrounded values from SQL and doing the rounding in .Net would be marginally more performant. Absolute faith corrupts as absolutely as absolute power Eric Hoffer The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not. Eric Hoffer

      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