By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Rounding

P: n/a
How do you round numbers in .NET? I have numbers defined as Single and after
some calculations I want to round the result to the nearest tenth so I can
do comparison of results (the rfuzz problem). Does FORMAT round?

--
------------------------------------
Wayne Wengert
wa***@wengert.org
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Wayne Wengert <wa***@wengert.org> wrote:
How do you round numbers in .NET? I have numbers defined as Single and after
some calculations I want to round the result to the nearest tenth so I can
do comparison of results (the rfuzz problem). Does FORMAT round?


If you want to compare results, then I suggest that rather than
rounding you just compare the difference between the expected results
and the ones you've got. You can specify the number of digits when you
format, but that'll use (I believe) bankers' rounding, which may or may
not be what you want.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2

P: n/a
Wayne Wengert wrote:
How do you round numbers in .NET? I have numbers defined as Single
and after some calculations I want to round the result to the nearest
tenth so I can do comparison of results (the rfuzz problem). Does
FORMAT round?


For rounding down:

System.Math.Floor(myNumber);
For rounding up:
System.Math.Ceiling(myNumber);
For simple rounding (the kind you do in maths classes at school):
System.Math.Round(myNumber);
Obviously these all return int types, and dont effect the parameter
variable.
Peace.
--
Phil Price
Student at The University Of Hull
http://www.philprice.net
http://www.dcs.hull.ac.uk
Jul 19 '05 #3

P: n/a
If you multiply the number by 10e10 before you round (using the Math.Round
above, or just CInt(num), then divide by 10e10, this will give you 10
decimal places.

Steven

"Phil Price" <ph**@philprice.net> wrote in message
news:be*********@newsg4.svr.pol.co.uk...
Wayne Wengert wrote:
How do you round numbers in .NET? I have numbers defined as Single
and after some calculations I want to round the result to the nearest
tenth so I can do comparison of results (the rfuzz problem). Does
FORMAT round?


For rounding down:

System.Math.Floor(myNumber);
For rounding up:
System.Math.Ceiling(myNumber);
For simple rounding (the kind you do in maths classes at school):
System.Math.Round(myNumber);
Obviously these all return int types, and dont effect the parameter
variable.
Peace.
--
Phil Price
Student at The University Of Hull
http://www.philprice.net
http://www.dcs.hull.ac.uk

Jul 19 '05 #4

P: n/a
Phil Price <ph**@philprice.net> wrote:
For simple rounding (the kind you do in maths classes at school):
System.Math.Round(myNumber);


No, that's *not* the kind of rounding most people do in maths classes
at school. At least, it certainly isn't the kind of rounding *I*
usually came across. It's bankers' rounding - halves round towards
even, so Math.Round(1.5)=2, Math.Round(2.5)=2, Math.Round(3.5)=4 etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.