如何简单地添加两位小数并四舍五入到最接近的两位小数?

How do I simply add two decimal places and round to the nearest two decimal places?

第一张图是教授要我们做的: https://prnt.sc/v1drxs

这是我的代码:

Console.Write("Enter the radius for a circle: ");

double radius = Convert.ToDouble(Console.ReadLine());
double diameter = 2 * radius;
double circumference = Convert.ToDouble(Math.PI * diameter);

Console.WriteLine("\nThe radius of the circle is " + radius + " units.");
Console.WriteLine("The diameter of the circle is " + diameter + " units.");
Console.WriteLine("The circumference of the circle is " + circumference + " units.");

Console.Write("\nPress any key to continue...");

Console.ReadKey();

我试过的:

我正在使用 Microsoft Visual Studio 2017。我对编码很陌生。帮助将不胜感激,谢谢!

您可以使用 Math.Round(myDouble, 2) 将值四舍五入到小数点后两位。

但是,如果该值四舍五入使得十进制值为“X.00”,那么这将输出“X”

据我所知,您希望它输出“X.00”

为此,我们可以使用String.Format方法。

以下是这 3 种情况的示例。

Console.WriteLine("The radius of the circle is " + radius + " units.");
Console.WriteLine("The diameter of the circle is " + diameter + " units.");
Console.WriteLine("The circumference of the circle is " + circumference + " units.");

Console.WriteLine();
Console.WriteLine("The radius of the circle is " + Math.Round(radius, 2) + " units");
Console.WriteLine("The diameter of the circle is " + Math.Round(diameter, 2) + " units"); 
Console.WriteLine("The circumference of the circle is " + Math.Round(circumference, 2) + " units");

Console.WriteLine();
Console.WriteLine(String.Format("The radius of the circle is {0:0.00} units", Math.Round(radius, 2)));
Console.WriteLine(String.Format("The diameter of the circle is {0:0.00} units", Math.Round(diameter, 2)));
Console.WriteLine(String.Format("The circumference of the circle is {0:0.00} units", Math.Round(circumference, 2)));

我会使用 内插字符串 格式说明符 。通过在字符串前面加上 $ 美元符号,您向编译器发出信号,表明该字符串在 {} 大括号内包含需要替换的变量。 F2 格式说明符表示“显示为小数点后两位”(F 代表 浮点数 )。您应该查找 标准 自定义数字格式说明符 .

请注意,我使用 double.TryParse 来解释用户的输入 - 如果输入有误,我会再给 him/her 一次机会。如果您使用 double.ParseConvert.ToDouble 来执行此操作,则输入不正确将导致抛出异常。用户以错误的格式输入信息几乎不是 例外 情况,因此应尽可能避免例外情况。 TryParse 调用用 bool 表示错误而不是异常。

double radius;
string responseFromUser;
do
{
    Console.Write("Enter the radius for a circle: ");
    responseFromUser = Console.ReadLine();
} while (!double.TryParse(responseFromUser, out radius));

double diameter = 2 * radius;
double circumference = Convert.ToDouble(Math.PI * diameter);

Console.WriteLine($"{Environment.NewLine}The radius of the circle is {radius:F2} units.");
Console.WriteLine($"The diameter of the circle is {diameter:F2} units.");
Console.WriteLine($"The circumference of the circle is {circumference:F2} units.");

Console.Write($"{Environment.NewLine}Press any key to continue...");

Console.ReadKey();

输出如下:

Enter the radius for a circle: abc
Enter the radius for a circle: 12.333

The radius of the circle is 12.33 units.
The diameter of the circle is 24.67 units.
The circumference of the circle is 77.49 units.

Press any key to continue...

如果您输入的内容少于两位小数,您仍然会得到两位小数:

Enter the radius for a circle: 4

The radius of the circle is 4.00 units.
The diameter of the circle is 8.00 units.
The circumference of the circle is 25.13 units.

Press any key to continue...