473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Doubles to Strings

nvx
Hi,
I'm looking for a simple way to convert a Double to a String exactly
the .ToString() does, except these two differences:
- the number of decimals is given (rounding is applied if
necessary), and
- trailing zeroes are kept.

This means I need it to be converted using the scientific notation if
the number is greater than a certain value etc., not just a simple
"this number of digits (or more if needed) before the decimal point
and precisely this number of decimals after the decimal point."

I could round the Double before converting it to a String
using .ToString(), but the trailing zeroes would be omitted.
Similarly, I could use the format string "0.00" or "0.00E+00", but the
scientific notation would not or would be used respectively (i.e.
converting 100.567 to "100.57" (which is OK) / "1.00E+02" (which means
a significant loss of data) and converting 123456789123456 789.12345 to
"12345678912345 6789.12" (awful...) / 1.23E+15 (this is the correct
conversion now)).

So, do I need to create an appropriate format string according to the
current Double value or is there some elegant solution of this?

Thanks for any help...

With regards
nvx

Mar 27 '07 #1
3 2384
You could make a separate function that takes in the double and
returns the string using your logic for if it is greater than a
certain value use the scientific notation if not use the other
formatting.

Or you could create a new class that inherits from double and
overrides the ToString method. This might be overkill though
depending on your exact situation.

Mar 27 '07 #2
Jeff T wrote:
You could make a separate function that takes in the double and
returns the string using your logic for if it is greater than a
certain value use the scientific notation if not use the other
formatting.

Or you could create a new class that inherits from double and
overrides the ToString method. This might be overkill though
depending on your exact situation.
Overkill and/or impossible (double is a struct; structs are inherently
sealed).

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Mar 28 '07 #3
nvx
Thanks both of you for your replies. It seems I'll have to code a bit
more...

With regards
nvx
Larry Lard napsal:
Jeff T wrote:
You could make a separate function that takes in the double and
returns the string using your logic for if it is greater than a
certain value use the scientific notation if not use the other
formatting.

Or you could create a new class that inherits from double and
overrides the ToString method. This might be overkill though
depending on your exact situation.

Overkill and/or impossible (double is a struct; structs are inherently
sealed).

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Mar 28 '07 #4

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

Similar topics

7
332
by: Daniel Lidström | last post by:
Hi, I'm currently using this method to extract doubles from a string: System::String* sp = S" "; System::String* tokens = s->Trim()->Split(sp->ToCharArray()); m_Northing = System::Double::Parse(tokens, nfi); m_Easting = System::Double::Parse(tokens, nfi); m_Elevation = System::Double::Parse(tokens, nfi);
1
346
by: Valerie Hough | last post by:
Is there a C# class that will help convert strings that include fractions ( "2 1/2" e.g.) to doubles ? Convert.ToDouble() throws an invalid input format exception. Obviously I can write code to do it but this seems like a fairly normal thing to want to do and I would expect C# to have already built in support. Can anyone help? Even...
0
1086
by: Sanjay Tibrewal | last post by:
Folks, I am still on 1.1 framework for various reasons. I have a DataColumn of doubles that I want to convert to an array of doubles. I can do that through a simple function but then I have to write a function for each type - one for int, string etc. Is there an inbuilt mechanism where I can have the system convert a DataColumn to a...
7
2684
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards Totto
116
35721
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
10
1965
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox txtradius that I enter a radius into. I then have a lable that I want the surface area to be displayed in called lblsurface
3
4006
by: RG | last post by:
I am reading in from a serial port a 16 bit number from 0 to 65536. It is being read as a string from my microcontroller into VB using DEC5 or 5 digits are displayed. I need to display this value live as a decimal number so I can manipulate the value. For instance I have the following line: TextBox2.Text = buffer1 - 32768 but this...
10
3219
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left is the string I get after tokenizing a bigger string. The number on the right is the number I get after the conversion.
2
1834
by: utab | last post by:
Dear all, On a formatted printing operation that I am trying to accomplish for doubles, I have a question. I would like to print a double, d, and its square, d*d, but I would like to make the argument to be computed upon the length of the numbers(d and d*d) so that it lines up neatly in the columns. For this operation, I need the number of...
0
7438
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...
0
7707
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. ...
0
7951
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...
1
7466
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...
0
7803
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...
0
6036
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...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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...

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.