Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数学。四舍五入-不四舍五入答案_C#_Math_Methods - Fatal编程技术网

C# 数学。四舍五入-不四舍五入答案

C# 数学。四舍五入-不四舍五入答案,c#,math,methods,C#,Math,Methods,我想做一个计算周长的程序。尽管如此,我还是更愿意用一个四舍五入的答案(小数点后两位)来代替小数点后七位。但是当我使用Math.Round方法时,它似乎并没有进行取整。我做错了什么 Console.Write("Circumference Calculator! Enter the radius of the circle: "); int inputRadius = Convert.ToInt32(Console.ReadLine()); double ans = Math.Sqrt(input

我想做一个计算周长的程序。尽管如此,我还是更愿意用一个四舍五入的答案(小数点后两位)来代替小数点后七位。但是当我使用
Math.Round
方法时,它似乎并没有进行取整。我做错了什么

Console.Write("Circumference Calculator! Enter the radius of the circle: ");
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + ans + " squared.");

您必须使用
Math.Round
的返回值,它不会引用变量

//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion

//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion

您必须使用
Math.Round
的返回值,它不会引用变量

//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion

//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion

您必须使用
Math.Round
的返回值,它不会引用变量

//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion

//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion

您必须使用
Math.Round
的返回值,它不会引用变量

//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion

//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion

Math.Round
不修改提供的参数-它返回一个新值。因此,您必须将其分配回一个变量以使其工作:

ans = Math.Round(ans, 2);

Math.Round
不修改提供的参数-它返回一个新值。因此,您必须将其分配回一个变量以使其工作:

ans = Math.Round(ans, 2);

Math.Round
不修改提供的参数-它返回一个新值。因此,您必须将其分配回一个变量以使其工作:

ans = Math.Round(ans, 2);

Math.Round
不修改提供的参数-它返回一个新值。因此,您必须将其分配回一个变量以使其工作:

ans = Math.Round(ans, 2);

通过不指定舍入结果,可以有效地计算舍入并在不使用它的情况下丢弃结果。一般来说,变量只有在分配给它们时才会发生变化(使用
=
),但传递变量时保持不变。请不要将文章的一半时间花在“感谢便笺”上,尽量提供尽可能小的示例。。。我试图删除一些与您的问题不严格相关的代码-如果您认为我删除了重要的细节,请随意回滚…请注意,您可能试图实现的是通常使用适当的字符串格式。通过不指定舍入结果,您正在有效地计算舍入,并在不使用它的情况下丢弃结果。一般来说,变量只有在分配给它们时才会发生变化(使用
=
),但传递变量时保持不变。请不要将文章的一半时间花在“感谢便笺”上,尽量提供尽可能小的示例。。。我试图删除一些与您的问题不严格相关的代码-如果您认为我删除了重要的细节,请随意回滚…请注意,您可能试图实现的是通常使用适当的字符串格式。通过不指定舍入结果,您正在有效地计算舍入,并在不使用它的情况下丢弃结果。一般来说,变量只有在分配给它们时才会发生变化(使用
=
),但传递变量时保持不变。请不要将文章的一半时间花在“感谢便笺”上,尽量提供尽可能小的示例。。。我试图删除一些与您的问题不严格相关的代码-如果您认为我删除了重要的细节,请随意回滚…请注意,您可能试图实现的是通常使用适当的字符串格式。通过不指定舍入结果,您正在有效地计算舍入,并在不使用它的情况下丢弃结果。一般来说,变量只有在分配给它们时才会发生变化(使用
=
),但传递变量时保持不变。请不要将文章的一半时间花在“感谢便笺”上,尽量提供尽可能小的示例。。。我试图删除一些与您的问题不严格相关的代码-如果您认为我删除了重要的细节,请随时回滚…请注意,您可能试图实现的是通常通过使用适当的字符串格式来实现的。