使用小数和整数
Working with Decimals and Integers
我确定这是一个愚蠢的问题,但我是新手!
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var myInt = ((1000/2048) * 1536);
Console.WriteLine(myInt);
}
}
}
输出为:
0
谁能告诉我如何获得正确的号码 (750)?
默认情况下,1000 和 2048 是 int,因此结果将被视为 int。
你必须把它们加倍。
选项 1:
var myInt = (((double)1000 / 2048) * 1536);
Console.WriteLine(myInt);
选项 2:
var myInt = ((1000.0 / 2048) * 1536);
Console.WriteLine(myInt);
您应该阅读 C# 中的值类型:
https://www.tutorialspoint.com/csharp/csharp_data_types.htm
https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx
我确定这是一个愚蠢的问题,但我是新手!
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var myInt = ((1000/2048) * 1536);
Console.WriteLine(myInt);
}
}
}
输出为: 0
谁能告诉我如何获得正确的号码 (750)?
默认情况下,1000 和 2048 是 int,因此结果将被视为 int。 你必须把它们加倍。
选项 1:
var myInt = (((double)1000 / 2048) * 1536);
Console.WriteLine(myInt);
选项 2:
var myInt = ((1000.0 / 2048) * 1536);
Console.WriteLine(myInt);
您应该阅读 C# 中的值类型:
https://www.tutorialspoint.com/csharp/csharp_data_types.htm
https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx