简体   繁体   English

如何在 0 和 1 之间获得小数?

[英]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 ,则需要使用后缀mM 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM