如何在文本字段中显示小数?
我正在创建一个简单的控制台应用程序,它显示 PI 的值,最多可达一定的小数位数。现在,我编写了以下代码:
namespace PIApplication
{
static void Main()
{
decimal Pi = Math.PI;
Console.Writeline("Pi is {0}.", PI);
}
}
我使用 {0}
的原因是我知道此方法在涉及布尔值时有效,但我只能假设 0 应该更改为其他值别的。有人能解释一下这在我的情况下是如何运作的吗?
此外,我收到一个错误,系统无法将双精度型转换为十进制型。我假设这是指定义为 PI 的值。我需要做什么才能将其从一种类型转换为另一种类型?我还必须这样做吗?
预先感谢您的所有帮助。
I am creating a simple console application that shows the value of PI up to a certain number of decimals. For now, I have written the following code:
namespace PIApplication
{
static void Main()
{
decimal Pi = Math.PI;
Console.Writeline("Pi is {0}.", PI);
}
}
The reason I used {0}
is that I know that this method works when it comes to Booleans but I can only assume that the 0 should be changed to something else. Can someone explain how this would work in my case?
Furthermore, I am getting an error that the system can not convert type double to decimal. I assume that this refers to the value that is defined as PI. What would I have to do to convert it from one type to the other? Do I even have to?
Thanks in advance for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是在 System.Math 中定义常量 PI 的方式:
尝试:
小数:与浮点类型相比,小数类型具有更高的精度和更小的范围,这使得它适合金融和金融领域货币计算。
This is how the constant
PI
is defined inSystem.Math
:Try:
Decimal: Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations.
不需要存储 PI,除非你想以给定的精度存储它。
这将给出 3.1415926536。
No need to store PI, unless you want to store it at a given precision.
This will give 3.1415926536.
显式转换??
强调文字
Explicitly convert??
emphasized text