473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 5685
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.