gpt4 book ai didi

c# - 在 C# 中使用 {#} 打印整数?

转载 作者:太空狗 更新时间:2023-10-29 22:21:40 24 4
gpt4 key购买 nike

如果这是重复的,我很抱歉,但我没有找到任何与之相关的内容。

那么,如何使用以下代码为具有整数平方根的数字打印0

for (n = 1.0; n <= 10; n++) 
{
Console.WriteLine ("Fractional Part : {0 :#.####}", (Math.Sqrt(n) - (int) Math.Sqrt(n)));
}

当前营业收入: enter image description here

最佳答案

假设其他小数结果的前导零是可以接受的,并且由于您的结果始终在 [0,1) 范围内,您可以只更改 #.### #0.####

for (var n = 1.0; n <= 10; n++)
{
Console.WriteLine("Fractional Part : {0:0.####}", (Math.Sqrt(n) - (int) Math.Sqrt(n)));
}

结果:

Fractional Part : 0
Fractional Part : 0.4142
Fractional Part : 0.7321
Fractional Part : 0
Fractional Part : 0.2361

关于c# - 在 C# 中使用 {#} 打印整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com