[英]How do I go about getting decimals between 0 and 1?
I'm trying to get all of the decimals with 3 digits in them with a while loop until it gets to 1.00.我正在尝试使用 while 循环获取所有包含 3 位数字的小数,直到达到 1.00。
Like so:像这样:
0.00 0.00
0.01 0.01
0.02 0.02
0.03 0.03
and so on.等等。
Any help is appreciated!任何帮助表示赞赏!
As @Mitch recommended in his comment, you should use decimal
primitive type:正如@Mitch 在他的评论中推荐的那样,您应该使用
decimal
原始类型:
for (decimal i = 0m; i <= 1; i += 0.01m)
{
Console.WriteLine(i);
}
If you want a numeric real literal to be treated as decimal
, you need to use the suffix m
or M
.如果您希望将数字实数视为
decimal
,则需要使用后缀m
或M
。 Without the suffix m
, the number is treated as a double
and generates a compiler error.如果没有后缀
m
,该数字将被视为double
并生成编译器错误。
You should post what you've tried already, but this should get you there:您应该发布您已经尝试过的内容,但这应该可以帮助您:
for (double i = 0; i <= 1; i += 0.01)
{
i = Math.Round(i, 2);
Console.WriteLine(i);
}
Good feedback in comments.评论中的良好反馈。 This should get you there.
这应该能让你到达那里。 Probably not the cleanest approach though.
虽然可能不是最干净的方法。
for(int i=0; i<=100; i++)
{
Console.WriteLine(string.Format("{0:F2}",i/100.0));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.