473,408 Members | 2,832 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,408 software developers and data experts.

math.round doesn't work

OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:

What I am seeing is the following:
Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93
Math.Round(4.935, 2, MidpointRounding.ToEven) 'Result is
4.93

Is this a bug? Am I just missing something?
I want 5 to round up which means AwayFromZero should do it right? In
this case even if I did ToEven it should go to 4.94 but it isn't. Is
it best to just abandon the round function in .Net and make my own. I
feel that rounding is such a basic function that I shouldn't have to
make my own function. Is this a floating point problem with .Net?
Right now I don't know if I can trust the round function that is built
in.

Nov 2 '06 #1
3 5304
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:

What I am seeing is the following:
Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93
Math.Round(4.935, 2, MidpointRounding.ToEven) 'Result is
4.93

Is this a bug? Am I just missing something?
I want 5 to round up which means AwayFromZero should do it right? In
this case even if I did ToEven it should go to 4.94 but it isn't. Is
it best to just abandon the round function in .Net and make my own. I
feel that rounding is such a basic function that I shouldn't have to
make my own function. Is this a floating point problem with .Net?
Right now I don't know if I can trust the round function that is built
in.
Nov 2 '06 #2
So it seems that this problem happens when you use the type double.
Should I just use the decimal type all the time? Is there a benefit of
using decimal over double or vice versa?

rowe_newsgroups wrote:
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:

What I am seeing is the following:
Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93
Math.Round(4.935, 2, MidpointRounding.ToEven) 'Result is
4.93

Is this a bug? Am I just missing something?
I want 5 to round up which means AwayFromZero should do it right? In
this case even if I did ToEven it should go to 4.94 but it isn't. Is
it best to just abandon the round function in .Net and make my own. I
feel that rounding is such a basic function that I shouldn't have to
make my own function. Is this a floating point problem with .Net?
Right now I don't know if I can trust the round function that is built
in.
Nov 2 '06 #3
I notice that if I do the following:
Math.Round(ctype(4.935, Decimal), 2, MidpointRounding.AwayFromZero)

I get the desired result. Is there any problem converting a double to
a decimal like this?

rowe_newsgroups wrote:
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:

What I am seeing is the following:
Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93
Math.Round(4.935, 2, MidpointRounding.ToEven) 'Result is
4.93

Is this a bug? Am I just missing something?
I want 5 to round up which means AwayFromZero should do it right? In
this case even if I did ToEven it should go to 4.94 but it isn't. Is
it best to just abandon the round function in .Net and make my own. I
feel that rounding is such a basic function that I shouldn't have to
make my own function. Is this a floating point problem with .Net?
Right now I don't know if I can trust the round function that is built
in.
Nov 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
8
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
5
by: Ark | last post by:
Hi everyone, Does anyone know if Direct3D overloads System.Math functions? Also is it possible to access the base functions of the overloaded function (in other words restore original of the...
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.