J
J.Marsch
Suppose that I have an integer division problem, but I want the answer was a
float. What's the cleanest syntax for that?
Example:
In this code, the variable z ends up == 0. And that is correct, because
this is integer division and the values are trunc'd. No problem. But
what's the most elegant code given that x and y have to be ints, and I want
z to be a float or double?
Code:
public static void Main()
{
int x = 50;
int y = 100;
float z = x / y; // z == 0, just like it's supposed to
Console.WriteLine(z.ToString());
}
Now, I know I could do this (below), but is there a better way?
public static void Main()
{
int x = 50;
int y = 100;
float x1 = x;
float y1 = y;
float z = x1 / y1; // z == 0.5, just like it's supposed to
Console.WriteLine(z.ToString());
}
float. What's the cleanest syntax for that?
Example:
In this code, the variable z ends up == 0. And that is correct, because
this is integer division and the values are trunc'd. No problem. But
what's the most elegant code given that x and y have to be ints, and I want
z to be a float or double?
Code:
public static void Main()
{
int x = 50;
int y = 100;
float z = x / y; // z == 0, just like it's supposed to
Console.WriteLine(z.ToString());
}
Now, I know I could do this (below), but is there a better way?
public static void Main()
{
int x = 50;
int y = 100;
float x1 = x;
float y1 = y;
float z = x1 / y1; // z == 0.5, just like it's supposed to
Console.WriteLine(z.ToString());
}