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

Rounding issue

I am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When
I multiply them together they come out to 3.705. I want to round this
to 2 decimals. It always rounds to 3.70 though. Even when I put
math.round(3.705,2) in the debug watch, I get 3.70. How can I get this
to round correctly? I know one option would probably be to add .00001
to the value before rounding, as I am sure this is floating point
issue, but that might cause a problem in the future with other data.

Oct 18 '06 #1
4 2373
Math.Round(3.705, 2, MidpointRounding.AwayFromZero)

Won't that work?

Thanks,

Seth Rowe
Altman wrote:
I am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When
I multiply them together they come out to 3.705. I want to round this
to 2 decimals. It always rounds to 3.70 though. Even when I put
math.round(3.705,2) in the debug watch, I get 3.70. How can I get this
to round correctly? I know one option would probably be to add .00001
to the value before rounding, as I am sure this is floating point
issue, but that might cause a problem in the future with other data.
Oct 18 '06 #2
Take a look at this link i think it will help you.

http://www.developerfusion.co.uk/show/4252/
Miro
"Altman" <ba*****@easy-automation.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>I am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When
I multiply them together they come out to 3.705. I want to round this
to 2 decimals. It always rounds to 3.70 though. Even when I put
math.round(3.705,2) in the debug watch, I get 3.70. How can I get this
to round correctly? I know one option would probably be to add .00001
to the value before rounding, as I am sure this is floating point
issue, but that might cause a problem in the future with other data.

Oct 18 '06 #3
Altman,

Maybe not so nice as program but it is very easy to do

dim a as double = 3.705
a + .005
dim b as double = Cdbl(a.ToString("d"))

I am not always sure from that "d" but if that is wrong have than a look at
the overloaded ToString for the correct patern.

(In net is used the ISO banking rounding, I never heard who it was using
outside that world and they use if definitley not here for their clients).

I hope this helps,

Cor

"Altman" <ba*****@easy-automation.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
>I am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When
I multiply them together they come out to 3.705. I want to round this
to 2 decimals. It always rounds to 3.70 though. Even when I put
math.round(3.705,2) in the debug watch, I get 3.70. How can I get this
to round correctly? I know one option would probably be to add .00001
to the value before rounding, as I am sure this is floating point
issue, but that might cause a problem in the future with other data.

Oct 18 '06 #4
I was unaware of the third parameter in the round function. It seems
to me that "AwayFromZero" should be the default setting but it
obviously is not that way.

rowe_newsgroups wrote:
Math.Round(3.705, 2, MidpointRounding.AwayFromZero)

Won't that work?

Thanks,

Seth Rowe
Altman wrote:
I am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When
I multiply them together they come out to 3.705. I want to round this
to 2 decimals. It always rounds to 3.70 though. Even when I put
math.round(3.705,2) in the debug watch, I get 3.70. How can I get this
to round correctly? I know one option would probably be to add .00001
to the value before rounding, as I am sure this is floating point
issue, but that might cause a problem in the future with other data.
Oct 18 '06 #5

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

Similar topics

7
by: Steven T. Hatton | last post by:
I'm surprised I haven't hit this situation till now, but I don't believe I've had to deal with it before. I have a function that sets the components of a point class (QPoint from Qt). It takes...
6
by: djc | last post by:
I have a sql field (MSDE 2000) of type Decimal. When inserting a value into the field from an asp.net web form I am using the CDec() function. If I enter a 2.1 through a 2.4 a 2.00 is entered into...
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
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...
5
by: Cygnus | last post by:
Sorry in advance for the lack of formatting in this posting. Data: (column headers) Net Sales | Royalty Rate | Total Royalty (data) 4.31 | 50.00% | 2.15 19.35 | 50.00% | 9.68
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
4
by: AMDRIT | last post by:
Given: ?System.Math.Round(161.5D,0) 162.0 ?System.Math.Round(162.5D,0) 162.0 ?System.Math.Round(163.5D,0) 164.0 In either C# '05 or VB.Net '05, can anyone explain the inconsistancy? Is
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) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.