473,404 Members | 2,178 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,404 software developers and data experts.

Using Rounding on a Double

Hi

I have a double that i want to round to a certain number of decimal places
this is the code i use

Return Math.Round(mdNumber, mbDecimalPlaces)

mbDecimalPlaces is the number of decimal places that i want, but it doesn't
use them with two decimal places i still get 0.0 or 9.9

Any ideas?
--
Kind Regards

Danny Woolston


Jul 19 '05 #1
3 5600
You could use the ([number]).ToString([number formatting expression]) like
so:

Dim TestNumber as Double = 1.2345
Dim strNumber as String
strNumber = TestNumber.ToString("#.00000")
'strNumber = "1.23450"
strNumber = TestNumber.ToString("#.00")
'strNumber = "1.23"
strNumber = TestNumber.ToString("00.#")
'strNumber = "01.2345"

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Danny Woolston <da*************@farmade.com> wrote:
I have a property of a control which will return a double, but the control allows the user to return a number of decimal places.

So if the decimal places are set to 2 and the number stored is 1.021 then on a call to the number property i want returned 1.02, or if the the number
stored is 1.3 and a decimal place of 3 then i want returned 1.300.

Let me stress that this is number only property i cannot use the format
property of the string.


Well you've got to - because doubles themselves don't *have* a number
of decimal places, they just have values. You can round a double to the
extent that rounding 1.021 to 2DP will round it to the closest double
value to 1.02, but there's no difference in the double itself between
1.5 and 1.50.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Jul 19 '05 #2
Hi

Thanks for the reply, but i need the number as a double not a string

--
Kind Regards

Danny Woolston
Software Developer

"Jonathan Amend" <ce*******@hotmail.com> wrote in message
news:3f15f0db$1_2@aeinews....
You could use the ([number]).ToString([number formatting expression]) like
so:

Dim TestNumber as Double = 1.2345
Dim strNumber as String
strNumber = TestNumber.ToString("#.00000")
'strNumber = "1.23450"
strNumber = TestNumber.ToString("#.00")
'strNumber = "1.23"
strNumber = TestNumber.ToString("00.#")
'strNumber = "01.2345"

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Danny Woolston <da*************@farmade.com> wrote:
I have a property of a control which will return a double, but the control allows the user to return a number of decimal places.

So if the decimal places are set to 2 and the number stored is 1.021 then on a call to the number property i want returned 1.02, or if the the number stored is 1.3 and a decimal place of 3 then i want returned 1.300.

Let me stress that this is number only property i cannot use the format property of the string.


Well you've got to - because doubles themselves don't *have* a number
of decimal places, they just have values. You can round a double to the
extent that rounding 1.021 to 2DP will round it to the closest double
value to 1.02, but there's no difference in the double itself between
1.5 and 1.50.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Jul 19 '05 #3
If you have .Net V1.1 you can try using the Decimal class. Unlike Double,
it does consider trailing decimal places as significant. It can also store
more data than Double and the precision for decimal number does not drop
off. However, it is bigger and slower than Double (16 bytes vs 8 bytes).

V1.0 did not support this feature on Double. It was changed to comply with
the new ECMA standard.

Jul 19 '05 #4

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

Similar topics

3
by: Dalan | last post by:
Is there any code available to address currency rounding problems in Access 97? Apparently, selecting currency type table fields does not resolve the problem. For instance, in my form I have a...
3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
3
by: Boz | last post by:
Hi, I am trying to use string.Format() to output the value of a double. double da = 100000000000.99994; double db = 100000000000.9994; double dc = 100000000000.994; double dd =...
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
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
10
by: alsmeirelles | last post by:
Hi all, I Have run this test: Private Sub test() Dim d As Double Dim f As Single Dim output As String d = 8888888888888.8887
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) ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.