473,385 Members | 1,587 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,385 software developers and data experts.

Question about rounding the decimal

ray
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

Oct 23 '06 #1
14 2953

"ray" <so*****@microsoft.comkirjoitti
viestissä:ud**************@TK2MSFTNGP02.phx.gbl...
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
Quite interesting.

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

-Teemu

Oct 23 '06 #2
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 23 '06 #3
ray
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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.googl egroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
>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

Oct 24 '06 #4
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 :-) )

Thanks,

Seth Rowe
ray wrote:
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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.googl egroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 24 '06 #5
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.Aw ayFromZero).ToString());

It shows 81.45.

But using a double shows 81.44

Oct 24 '06 #6
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.

ray wrote:
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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.googl egroups.com...
>Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
>>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

Oct 24 '06 #7
ray wrote:
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
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.
Oct 24 '06 #8
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 wrote:
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.

ray wrote:
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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.googl egroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 25 '06 #9
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.

rowe_newsgroups wrote:
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 wrote:
>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.

ray wrote:
>>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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.goo glegroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 25 '06 #10
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.

rowe_newsgroups wrote:
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 wrote:
>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.

ray wrote:
>>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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.goo glegroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 25 '06 #11
Oops... sent that one prematurely by mistake.

Göran Andersson wrote:
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.
Oct 25 '06 #12
Wow, great explaination.

Thanks,

Seth Rowe
Göran Andersson wrote:
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.

rowe_newsgroups wrote:
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 wrote:
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.

ray wrote:
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" <ro********@yahoo.com>
???????:11**********************@f16g2000cwb.goog legroups.com...
Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)

Thanks,

Seth Rowe
ray wrote:
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
Oct 25 '06 #13


*** Sent via Developersdex http://www.developersdex.com ***
Oct 30 '06 #14
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.

*** Sent via Developersdex http://www.developersdex.com ***
Oct 30 '06 #15

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

Similar topics

9
by: GuineaPig | last post by:
Hello, I'm (very) new to c++ and I'm having trouble understanding why this doesn't work. Here's some testcode: #include <iostream.h> int main() { float test;
5
by: lamthierry | last post by:
Are the following two similar? float a = 1.0f; float a = static_cast<float>(1.0); If they are, which one is preferrable? Thanks Thierry
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. ...
19
by: morc | last post by:
hey, I have float values that look something like this when they are printed: 6.0E-4 7.0E-4 I don't want them to be like this I want them to be normalized with 4 decimal places.
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...
6
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, I'm using the following on a form in my database: =(-Int(-Sum((IIf(=Yes,((*)+()), (((*)+)*1.06))*100))))/100 In this case, I want to calculate a total for an order. If...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
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) ...
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: 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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.