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

Convert string to Double

Hi every one!

I have a funny problem , I have this piece of code which should convert a string to double and again Double to string.

try
{
string str = "98.22";
double d = Double.Parse(str);
d = d - 1;
str = d.ToString();
Console.Write(str);
}
catch (Exception e)
{
Console.Write(e.Message);
}

but i get an exception which says: Input string was not in correct format.
Do you know what the problem is?

Thank U
Nov 2 '07 #1
4 1755
SammyB
807 Expert 512MB
Your code works fine for me and shows 97.22.
Nov 3 '07 #2
balabaster
797 Expert 512MB
Your code works fine for me and shows 97.22.
I'm going to hazard a guess that due to the fact it works on one machine and not on another that it has to do with cultural differences between machines. Try something like this:
VB
Expand|Select|Wrap|Line Numbers
  1. Dim MyDouble As Double 
  2. MyDouble = Double.Parse("14.02", _
  3.   System.Globalization.GetCultureInfo(1033).NumberFormat)
  4. Console.WriteLine(MyDouble ^ 2)
C#
Expand|Select|Wrap|Line Numbers
  1. Double MyDouble = Double.Parse("14.02", System.Globalization.CultureInfo.GetCultureInfo(1033).NumberFormat);
  2. Console.WriteLine(MyDouble ^ 2);
I get the result 196.5604

Note of interest 1033 is English U.S. and as such, this code would translate the string as a U.S. string using the "." as a decimal point. If we were to change to French or another culture, it would read the "." as a thousands separator and "," as a decimal point.

Hope that helps
Nov 3 '07 #3
r035198x
13,262 8TB
I'm going to hazard a guess that due to the fact it works on one machine and not on another that it has to do with cultural differences between machines. Try something like this:
VB
Expand|Select|Wrap|Line Numbers
  1. Dim MyDouble As Double 
  2. MyDouble = Double.Parse("14.02", _
  3.   System.Globalization.GetCultureInfo(1033).NumberFormat)
  4. Console.WriteLine(MyDouble ^ 2)
C#
Expand|Select|Wrap|Line Numbers
  1. Double MyDouble = Double.Parse("14.02", System.Globalization.CultureInfo.GetCultureInfo(1033).NumberFormat);
  2. Console.WriteLine(MyDouble ^ 2);
I get the result 196.5604

Note of interest 1033 is English U.S. and as such, this code would translate the string as a U.S. string using the "." as a decimal point. If we were to change to French or another culture, it would read the "." as a thousands separator and "," as a decimal point.

Hope that helps
No. It has nothing to do with cultural differences.
The code works fine and produces the same results on all machines running the .NET framework.
Nov 3 '07 #4
pbalaj
1
Hi Dear,

Your code works fine for me...

thanks,
Bala
Nov 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
2
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...
2
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...
0
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",...
17
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...
2
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...
6
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...
4
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...
15
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...
1
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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,...
0
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...
0
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...
0
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...

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.