Reputation: 69
So I'm trying to make a simple CSGO Item value to key value calculator. I'm a beginner programmer and I'm getting this error. And after googling through this error I can't seem to find the problem. Anyone who can help?
private void button1_Click(object sender, EventArgs e)
{
int itemValue = int.Parse(textBox1.Text);
int Keys = itemValue * 0.8 / 2.15;
label2.Text = "Value of item in keys:" + Keys;
}
Upvotes: 2
Views: 6047
Reputation: 2436
I'm kind of sad that this question's been downvoted. Yes, it's a simple question that's likely already been answered here, but as someone new to the language, how can you know what to search for? :)
See this article for an explanation of casting and type conversions, and an example similar to your problem.
The error is on the line int Keys = itemValue * 0.8 / 2.15;
.
This looks simple, but there are a few parts to it. Let's break it down.
int Keys
This first part is the variable declaration. You're declaring a variable called Keys
. Its type is int
. Pretty straightforward.
The second part is the assignment operator =
. It's saying "I want Keys
to be this value." Again, pretty simple.
The third part is the evaluation of everything on the right side of =
, which boils down to some number with a decimal, right?
Your problem is that you've declared Keys
as an int
, but itemValue * 0.8 / 2.15
is of type double
, a different number format, that can't be converted to an integer (what would the program do with the decimal part of the number?).
So, you need to declare Keys
as "double Keys
" so it can store your result, or you need to get an integer value from your decimal number. You can do that by casting, or by using the Round
, Ceiling
, or Floor
methods in the Math
class.
Upvotes: 6
Reputation: 53958
This itemValue * 0.8 / 2.15
has as a result a double. You can't assign a double to an int.
You could overcome this like below:
int Keys = (int)(itemValue * 0.8 / 2.15);
which will cast this result to an int
.
Update
I don't know if this is your intention. Pay attention on this that you will lose in accuracy doing so.
For example, the following console application
public class Program
{
public static void Main()
{
int itemValue = 2;
int Keys = (int)(itemValue * 0.8 / 2.15);
Console.WriteLine(Keys);
double dKeys = itemValue * 0.8 / 2.15;
Console.WriteLine(dKeys);
}
}
outputs:
0
0.744186046511628
Please have a look here.
So it depends on you, if you want to cast your result in an int or use the double. I mean we don't know what's Keys
.
Upvotes: 1
Reputation: 2022
What you are trying to do is do assign to an int variable a double value.
C# knows that, in that way you may lose data. Then it does not permit to do that.
What you can do is to change you code like this:
private void button1_Click(object sender, EventArgs e)
{
double itemValue = Double.Parse(textBox1.Text);
double Keys = itemValue * 0.8 / 2.15;
label2.Text = "Value of item in keys:" + Keys;
}
Or, if you really know what you are doing:
private void button1_Click(object sender, EventArgs e)
{
int itemValue = Convert.ToInt32(textBox1.Text);
int Keys = (int) (itemValue * 0.8 / 2.15);
label2.Text = "Value of item in keys:" + Keys;
}
but with this, you may lose value
You can read mode about this here: http://msdn.microsoft.com/en-us/library/ms173105.aspx
Upvotes: 0