473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(mdNu mber, 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 5634
You could use the ([number]).ToString([number formatting expression]) like
so:

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

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.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.co m>
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*******@hotm ail.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.ToSt ring("#.00000")
'strNumber = "1.23450"
strNumber = TestNumber.ToSt ring("#.00")
'strNumber = "1.23"
strNumber = TestNumber.ToSt ring("00.#")
'strNumber = "01.2345"

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.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.co m>
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
9425
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 price of item field (say $49.95), and a percentage discount field (say 10% = $5.00), and calculated net cost field of the two. Access seemingly doesn't understand banker's rules as the resulting total is $44.96. Could this be a bug? Why does it...
3
2713
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 dynamic memory required for my datastructures ( used sizeof() to get the size of the structures ). But mallinfo() function show that approximately double that amount of memory is being allocated. I am working with gcc 3.2.2 on a redhat 9 machine. I...
3
6746
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 integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is
3
12433
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 = 1000000000.99994; Debug.WriteLine(string.Format("a={0:F10}", da));
29
3194
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
6196
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
10
2172
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
8018
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
13349
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
20
5023
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
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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
10097
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...
0
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.