473,782 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert double to string

I want to convert a double value to string.

1.2345678911 -> "1.23456789 1"
123 -> "123"

I used the following code to convert it:

string str = doubleValue.ToS tring("f9");

but the second value is converted to "123.000000000" .

So I changed the code to:
string str = doubleValue.ToS tring("f9").Tri mEnd('0').TrimE nd('.');
It works fine.

But I want to know if there is a better solution. Could anybody give me a
format string so I can use the following code to do the same thing as above?

string str = doubleValue.ToS tring(formatStr ing);
Nov 16 '05 #1
2 27319
Laurence <ha********@hot mail.com> wrote:
I want to convert a double value to string.

1.2345678911 -> "1.23456789 1"
123 -> "123"

I used the following code to convert it:

string str = doubleValue.ToS tring("f9");

but the second value is converted to "123.000000000" .

So I changed the code to:
string str = doubleValue.ToS tring("f9").Tri mEnd('0').TrimE nd('.');
It works fine.
No it doesn't - try giving it "1230" and you'll get back "123".
But I want to know if there is a better solution. Could anybody give me a
format string so I can use the following code to do the same thing as above?

string str = doubleValue.ToS tring(formatStr ing);


Try "0.######## #" as the format string. I think that does what you
want.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Great. Thank you.
"Jon Skeet [C# MVP]" <sk***@pobox.co m>
??????:MP****** *************** ***@msnews.micr osoft.com...
Laurence <ha********@hot mail.com> wrote:
I want to convert a double value to string.

1.2345678911 -> "1.23456789 1"
123 -> "123"

I used the following code to convert it:

string str = doubleValue.ToS tring("f9");

but the second value is converted to "123.000000000" .

So I changed the code to:
string str = doubleValue.ToS tring("f9").Tri mEnd('0').TrimE nd('.');
It works fine.


No it doesn't - try giving it "1230" and you'll get back "123".
But I want to know if there is a better solution. Could anybody give me a
format string so I can use the following code to do the same thing as
above?

string str = doubleValue.ToS tring(formatStr ing);


Try "0.######## #" as the format string. I think that does what you
want.

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

Nov 16 '05 #3

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

Similar topics

4
47071
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
2
4964
by: Pascal | last post by:
Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll Additional information: Value was either too large or too small for a Double. Pascal
2
11528
by: paul gao via .NET 247 | last post by:
hi all. In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class will only need to handle numbers that increment by1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the doublenumber is 0.28 which is between 1/4 and 9/32, the string shouldbe 9/32(go with the bigger number). TIA --------------------------------...
0
8431
by: R. John Reed | last post by:
Hi All, I'm am looking to convert a currency string (e.g. "$1,234.56" to a double value). It appears this will work: double val = Convert.ToDouble(Double.Parse­("$123,456.78901", System.Globalization.NumberSty­les.Currency));
17
4378
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
2
6632
by: Jason | last post by:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string variable to double variable, I got something interesting: Dim stringVal As String = "101.01" Dim doubleVal As Double doubleVal = System.Convert.ToDouble(stringVal) Msgbox (doubleVal) will return 101.01 but...... Dim stringVal As String = "101.00"
6
1407
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function declaration statments (first lines) e.g. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String Private Function ConvertHundreds(ByVal MyNumber As String) As String etc.
4
4524
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
15
7658
by: shiniskumar | last post by:
How to convert string to double? Ive got a double variable dTot; its value is 5.037717235E7 when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35". now i want to pass it as a double value to the method format() which is shown below. when i do Double.parseDouble i get dTot as 5.037717235E7 CurrencyConverter.format(Double.parseDouble(FreemarkerTools.formatDecimal(dTot)));
1
4080
by: Bjorn Brox | last post by:
Hi! In germany, norway and France(?) we are using ',' as decimal separator and it always messes up when you convert a double to and from a string where the interface expects double values stored as string is using '.' What parameter shall I use in double.ToString() to ensure that the outputstring always are using '.' as separator without thousand sep, and what parameter shall I use to convert double to Double.Parse() or...
0
9474
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
10308
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...
0
10143
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
10076
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
8964
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
7486
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
6729
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();...
0
5375
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...
3
2870
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.