Problem in Converting Decimal To String in C#.Net

15,304

Solution 1

The first method is trying to take a decimal (the result of adding the 2 decimals) and cast it as a string. Since there's no (implicit or) explicit conversion from decimal to string, it throws because of the mismatch.

The second one takes a decimal and calls a ToString() method on it - since ToString is a valid method on the decimal type, this makes a normal instance method call and you get the return value of that call, which is a string.

Since you're using Convert calls already, you might find it more natural to do Convert.ToString to get the decimal back to a string.

It might be more clear if you separate the 'add two decimals' to a separate local var, since that's common to both here.

So, the (commented out) total1 fails because it's trying to just cast, and we have no conversion available to do so. The latter two both work fine, since they are method calls that are returning a string.

string a = "100.00", b = "50.00";
decimal result = Convert.ToDecimal(a) + Convert.ToDecimal(b);
//string total1 = (string)result;
string total2 = result.ToString();
string total3 = Convert.ToString(result);

Solution 2

In the first method are you basically doing

string a = (string)150m;

where 150 is decimal. There is no explicit or implicit cast from decimal to string. Compiler will not understand how to do the convert.

In the second method you call

string a = 150m.ToString();

In this case you are calling ToString method that Decimal class implements (along with all other classes).

Solution 3

The first code tries to "type cast" a decimal to string, where as the second one calls the ToString method on decimal to get the string representation of decimal. Type casting from decimal to string won't work, unless decimal type overloads the type conversion operator which it doesn't and hence the type casting throws exception.

Type casting in this case is like asking the CLR system to type cast a decimal to string, which CLR is unable to do because it doesn't find a way to do it. Calling ToString is asking the decimal type to returns its string representation

Solution 4

Method1 is a cast , so you are trying to 'read' the data contained in a decimal variable as a string...of course you can't. You can cast, for example, a byte to an int because they are both numbers. Instead for decimal to string you need an explicit conversion procedure.

Solution 5

its because in first method you converted string a and b into decimal but while then you are just type casting to string, you need to convert that decimal value into string before assigning it to string variable. in second method you are actually converting decimal value into string by using ToString() method, so its not giving error.

Share:
15,304
thevan
Author by

thevan

Software Engineering Senior Analyst at Accenture Solutions Private Limited, Chennai, India. Interested in ASP.Net, MVC, Web API, WCF, Web Services, ADO.Net, C#.Net, VB.Net, Entity Framework, MS SQLServer, Angular.js, JavaScript, JQuery, Ajax, HTML and CSS

Updated on June 14, 2022

Comments

  • thevan
    thevan almost 2 years

    While Converting Decimal to String, I tried two methods.

    Method 1:

        string a = "100.00", b = "50.00";
        string Total = (string)(Convert.ToDecimal(a) + Convert.ToDecimal(b));
    

    It throws error, cannot convert Decimal to String.

    Method 2:

        string Total = (Convert.ToDecimal(a) + Convert.ToDecimal(b)).ToString();
    

    It doesn't throw error and it is working fine.

    I want to know the difference between these two methods of Conversion and Why it throws error when I used Method 1?

  • James Manning
    James Manning almost 13 years
    nit-pick, but while System.Object defines ToString() (obviously), the actual implementation being used is of an override created in System.Decimal. If it didn't do so, the result would have been a string like "System.Decimal" since the object ToString() prints out the type name.