Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class converstion
{

    public static void Main(string[] args)
{
string s1="sidd";
string s2="siddh";



double da=Convert.ToDouble(s1);
double d=Convert.ToDouble(s2);
Console.WriteLine("converte string is={0}",da);
Console.WriteLine("converte string is={1}", d);

}

}

it compile successfullly but not run...error "Unhandel Exceptuon:System.FormatException:input string was not in a correct formate
Posted
Updated 27-Sep-12 23:46pm
v2
Comments
JF2015 28-Sep-12 5:38am    
You are trying to convert the string "sidd" to a double - how is this supposed to work?
Abhijit Parab 28-Sep-12 5:39am    
how a string can be converted into double data type. Are you aware of the Data types and type casting?
CPallini 28-Sep-12 5:45am    
You should use the MagicConvert.ToDouble() method, I suppose
siddharth629 28-Sep-12 6:04am    
lol

Exception is correct. How can you convert a string like "sidd" to a double.
Try converting "10" to double, it will work.
 
Share this answer
 
Comments
siddharth629 28-Sep-12 5:45am    
sir is it is possible to convert a string into integer . like "sid" to integer
nagendrathecoder 28-Sep-12 5:46am    
You cannot convert string like "sid" to an integer, not even in imagination.
If u want to convert a string to a number , the values of the string must be number.

Eg : string str = "445"; // Possible
string str = "abcd"; // Not Possible
 
Share this answer
 
Hi Siddharth,
try to understand. you can not change this type of string value into double. beacuse this is real string. How can you change your name into double. is this possible. You can do this...


C#
String str="50";
double doubleValue=Convert.ToDouble(str);


and now it will return you to a double value....

Regards Rahul
 
Share this answer
 
Comments
siddharth629 28-Sep-12 5:48am    
thanks sir
You can use the TryParse methods
so your code should look like:

C#
public static void Main(string[] args)
{
string s1="sidd";
string s2="siddh";

double da= = 0;
double d=0;
if (Double.TryParse(s1, out da))
    Console.WriteLine("converte string is={0}",da);
if (Double.TryParse(s2, out d)
    Console.WriteLine("converte string is={0}", d);

}


This mans that if the conversion fails da and d have value 0 otherwise the converted value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900