473,508 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rounding

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
4 5697
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6717
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
4
7816
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
8
2067
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
2
2598
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 ...
11
6620
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
18
2195
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
5
7989
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
13093
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
4958
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
29451
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.