vb.net 2003 rounding decimal value question

  • Thread starter Thread starter spebola
  • Start date Start date
S

spebola

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. I need it to contain 180.26.

Any ideas?
 
Hi Spebola,
It is strange,
dim Amount as decimal = 180.255
Amount = Amount.Round(Amount, 2)
Amount now contains 180.25. I need it to contain 180.26.

I did try it and the result from me was 180.26, is there maybe a mouse on
your computer eating bits?

This is from MSDN
When d is exactly halfway between two rounded values, the result is the
rounded value that has an even digit in the far right decimal position. For
example, when rounded to two decimals, the value 2.345 becomes 2.34 and the
value 2.355 becomes 2.36. This process is known as rounding toward even, or
rounding to nearest.

Cor
 
(e-mail address removed) (spebola) scripsit:
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. I need it to contain 180.26.

Any ideas?

This code works for me (Windows XP Professional, .NET 1.1):

\\\
Dim Amount As Decimal = 180.255D
Amount = Amount.Round(Amount, 2)
MsgBox(Amount.ToString())
///

Add 'Option Strict On' on top of your source file!
 
Hi Spebola,

I can not reproduce the problem,
My test code and result is as follows.
Module Module1
Sub Main()
Dim Amount As Decimal = 180.255
Amount = Amount.Round(Amount, 2)
Console.WriteLine(Amount)
End Sub
End Module
Output: 180.26

Did the problem persists with all number, such as , 180.177,180.173?
You may have a try to see if the problem is a common issue in your machine.
I will appricate the problem.
I look forward to hearing from you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Hi Spebola,

This behavior has been stated in the msdn.
Decimal.Round Method
When d is exactly halfway between two rounded values, the result is the
rounded value that has an even digit in the far right decimal position. For
example, when rounded to two decimals, the value 2.345 becomes 2.34 and the
value 2.355 becomes 2.36. This process is known as rounding toward even, or
rounding to nearest.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemMathClassAbsTopic.asp

The behavior also exist in VB6.
189847 INFO: New String and Format Functions in Visual Basic 6.0
http://support.microsoft.com/?id=189847

You may refer to link below for more details about business rule.
http://www.uop.edu/cop/psychology/Statistics/Rounding.html
According to Hurlburt, 1994 pg. 12,the procedure for finding the last digit
of a
measure is called "Rounding".
Rule 3- To prevent rounding bias, if the remainder is exactly 5, then round
the
last digit to the closest even number.Thus the number 3.55 (rounded to 1
digit)
would be 3.6 (rounding up) and the number 6.450 would round to 6.4
(rounding
down)if rounding to 1 decimal.
See page 12 in Hurlburt, R. (1994) Comprehending Behavioral Statistics,
Brooks/Cole, Pacific Grove, CA.

This response contains a reference to a third-party World Wide Web site.
Microsoft is providing this information as a convenience to you. Microsoft
does not control these sites and has not tested any software or information
found on these sites; therefore, Microsoft cannot make any representations
regarding the quality, safety, or suitability of any software or
information found there. There are inherent dangers in the use of any
software found on the Internet, and Microsoft cautions you to make sure
that you completely understand the risk before retrieving any software from
the Internet.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Back
Top