Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'

Tags:

c#

I need convert String of input of table database to Integer value in C# .NET 4 and tried this code inspired from this Link:

    int i;
    string Entry_Level = Convert.ToInt32("2,45");
    i = Convert.ToInt32(Entry_Level); 

But I've this error:

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'

EDIT

Solved with:

    decimal i;
    string Entry_Level = "2,45";
    i = Convert.ToDecimal(Entry_Level);

    Response.Write(i.ToString());
    Response.End();

In output I've 2,45, many thanks!

like image 562
Chevy Mark Sunderland Avatar asked Oct 19 '25 14:10

Chevy Mark Sunderland


1 Answers

string Entry_Level = Convert.ToInt32("2,45");

should be

string Entry_Level = "2,45";

Why not go for this though:

int i = 2,45;

But since this is not an integer, you'll need one of the built-in decimal types:

/* use this when precision matters a lot, for example when this is a unit price or a percentage value that will be multiplied with big numbers */
decimal i = 2.45 


/*  use this when precision isn't the most important part. 
It's still really precise, but you can get in trouble when dealing with really small or really big numbers. 
Doubles are fine in most cases.*/
double i = 2.45 

See this thread for more information about decimal vs double.

like image 198
Moeri Avatar answered Oct 22 '25 03:10

Moeri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!