473,625 Members | 3,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2979

"ray" <so*****@micros oft.comkirjoitt i
viestissä:ud*** ***********@TK2 MSFTNGP02.phx.g bl...
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, MidpointRoundin g.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, MidpointRoundin g.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.4 45,
2,MidpointRound ing.AwayFromZer o), 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_newsgroup s" <ro********@yah oo.com>
???????:11***** *************** **@f16g2000cwb. googlegroups.co m...
Math.Round((31* 1.555),2, MidpointRoundin g.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, MidpointRoundin g.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.4 45,
2,MidpointRound ing.AwayFromZer o), 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_newsgroup s" <ro********@yah oo.com>
???????:11***** *************** **@f16g2000cwb. googlegroups.co m...
Math.Round((31* 1.555),2, MidpointRoundin g.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 ,MidpointRoundi ng.AwayFromZero ).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, MidpointRoundin g.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.4 45,
2,MidpointRound ing.AwayFromZer o), 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_newsgroup s" <ro********@yah oo.com>
???????:11***** *************** **@f16g2000cwb. googlegroups.co m...
>Math.Round((31 *1.555),2, MidpointRoundin g.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, MidpointRoundin g.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.4 45,
2,MidpointRound ing.AwayFromZer o), 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_newsgroup s" <ro********@yah oo.com>
???????:11***** *************** **@f16g2000cwb. googlegroups.co m...
Math.Round((31* 1.555),2, MidpointRoundin g.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.0000000000000 5%.

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, MidpointRoundin g.AwayFromZero) and
the problem is solved.
However, when I tried Math.Round(81.4 45,
2,MidpointRou nding.AwayFromZ ero), 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_newsgro ups" <ro********@yah oo.com>
???????:11*** *************** ****@f16g2000cw b.googlegroups. com...
Math.Round(( 31*1.555),2, MidpointRoundin g.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

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

Similar topics

9
7290
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
1352
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
7821
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. I need it to contain 180.26. Any ideas?
19
4671
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
6636
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 seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
6
2244
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 the LineTaxExempt field is checked (meaning tax exempt), the total is
29
3161
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
8003
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 raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not
206
13215
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) It then updates the value with numberOfPrecisions after the decimal
30
29543
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 expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6118
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.