Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about rounding the decimal

196 views
Skip to first unread message

ray

unread,
Oct 23, 2006, 6:12:32 AM10/23/06
to
Dear all,
In my vb.net program, I have to round a decimal number to two decimal
place.
For example, for the expression "Decimal.Round(31* 1.555, 2)", I should
get the result "48.21". However, the program finally give "48.20". Is there
any mistake for my expression? Please help.
Thanks a lot,
Ray


Teemu

unread,
Oct 23, 2006, 6:39:45 AM10/23/06
to

"ray" <som...@microsoft.com> kirjoitti
viestissä:udTIAvo9...@TK2MSFTNGP02.phx.gbl...

Quite interesting.

I can't say why it works like that but you could try
Format(31*1.555,"0.00").

-Teemu

rowe_newsgroups

unread,
Oct 23, 2006, 6:45:03 AM10/23/06
to
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe

ray

unread,
Oct 23, 2006, 11:30:06 PM10/23/06
to
Dear all,
I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.445,
2,MidpointRounding.AwayFromZero), I suppose that the program should give me
81.45 but actually it give me 81.44. Is there any mistake for my expression?
Please help.
Thanks a lot,
Ray

"rowe_newsgroups" <rowe_...@yahoo.com>
???????:1161600302.9...@f16g2000cwb.googlegroups.com...

rowe_newsgroups

unread,
Oct 24, 2006, 6:55:55 AM10/24/06
to
That is very odd! It seems to not like .445 as the following all round
up:

.345 = .35
.545 = .55
.455 = .46
.435 = .44

I have no idea why it's rounding down .445. The documentation states
"The behavior of this method follows IEEE Standard 754, section 4"
perhaps something in the IEEE documentation will explain it. I'll do
some more research and let you know what I find. (Unless someone else
wants to share :-) )

Chris Dunaway

unread,
Oct 24, 2006, 10:45:07 AM10/24/06
to
rowe_newsgroups wrote:
> That is very odd! It seems to not like .445 as the following all round
> up:
>
> .345 = .35
> .545 = .55
> .455 = .46
> .435 = .44
>
> I have no idea why it's rounding down .445. The documentation states
> "The behavior of this method follows IEEE Standard 754, section 4"
> perhaps something in the IEEE documentation will explain it. I'll do
> some more research and let you know what I find. (Unless someone else
> wants to share :-) )
>

It must be a issue with using doubles. When I use this code:

decimal d = 81.445m;
MessageBox.Show(Math.Round(d,2,MidpointRounding.AwayFromZero).ToString());

It shows 81.45.

But using a double shows 81.44

Göran Andersson

unread,
Oct 24, 2006, 2:23:26 PM10/24/06
to
As always with floating point math, there are almost no values that can
be represented exactly. The closest value to 81.445 that can be
represented is probably something like 81.444999999, which of course
rounds to 81.44.

Göran Andersson

unread,
Oct 24, 2006, 2:27:43 PM10/24/06
to

The precision is lost either in the calculation or the conversion. First
you multiply two Double values, which most likely results in a value
that is not exact. Then you convert the Double value into Decimal, which
also can introduce rounding errors.

rowe_newsgroups

unread,
Oct 25, 2006, 6:52:14 AM10/25/06
to
I just wish I knew how it calculated the value, and why it only rounds
down with .445. Any other double that ends with 45 (.345, .545, etc)
rounds up. Any ideas on the math that goes on behind the scenes?

Thanks,

Seth Rowe

Göran Andersson

unread,
Oct 25, 2006, 3:11:23 PM10/25/06
to
Not exactly, but something like this:

The value 31 is represented as a floating point number as closely to
0.96875*2^5 as possible. The mantissa and exponent are stored binary,
which means that 0.96875 is stored as 1/4 + 1/32 + 1/64 + 1/128 + 1/256
+ 1/1024 + ... etc.

3.875 * 2^3

(I'm not sure if that is totally accurate, but you see how it works.)

A Double value is accurate to 15 digits, so usually any value is off by
something like 0.00000000000005%.

So, even though 31 can easily be represented with total accuracy as an
integer, it's very unlikely that it's stored as exactly 31 as a floating
point number.

Göran Andersson

unread,
Oct 25, 2006, 3:34:01 PM10/25/06
to
Not exactly, but something like this:

The value 31 is represented as a floating point number as closely to
0.96875*2^5 as possible. The mantissa and exponent are stored binary,

which means that 0.96875 is stored as 1/2 + 1/4 + 1/8 + 1/16 + ... etc.

(I'm not sure if that is totally accurate, but you see how it works.)

A Double value is accurate to 15 digits, so usually any value is off by
something like 0.00000000000005%.

So, even though a number like 31 can easily be represented with total
accuracy as an integer, it's very unlikely that it's stored exactly as a
floating point number.

So it goes for every number and every calculation, so the final result
is seldom exactly what you expect, but mostly so close that you never
see the difference.

It's only when you expect a value like 81.445 to be exactly 81.445
instead of something like 81.4449999999999, that you see the difference.

Göran Andersson

unread,
Oct 25, 2006, 3:34:37 PM10/25/06
to
Oops... sent that one prematurely by mistake.

rowe_newsgroups

unread,
Oct 25, 2006, 4:32:56 PM10/25/06
to
Wow, great explaination.

Amjad Khan

unread,
Oct 30, 2006, 7:01:56 AM10/30/06
to

*** Sent via Developersdex http://www.developersdex.com ***

Amjad Khan

unread,
Oct 30, 2006, 7:08:52 AM10/30/06
to
Hi,

I had the same problem with rounding.There is bug with Math.Round method
in .NET.

when u tried Math.Round(81.445,2,MidpointRounding.AwayFromZero),it will
give you always 81.44. to get the value 81.45 but you will have to write
seperate function for roundiing.

for example:

public static double RoundToTwoDecimalPlace(double dbValue,int Position)
{
decimal dc = (decimal)dbValue;
System.Data.SqlTypes.SqlDecimal d =
System.Data.SqlTypes.SqlDecimal.Round(dc, Position);
//Math.Floor(db+.000005);

dbValue = Convert.ToDouble(d.Value);
return dbValue;
}

when you call this function it will round it from SQL DB type and it
will give correct result.

Try this method it will solve your problem.

0 new messages