首页 热点资讯 义务教育 高等教育 出国留学 考研考公

c#字符串里的数字可以定义成变量吗 如"{0:f3}"中的3

发布网友 发布时间:2小时前

我来回答

3个回答

热心网友 时间:1小时前

我们可以看到其中的格式说明"{0:f3}"是一个字符串,那么我们就可以用字符串的“加法”编写咯。
Console.WriteLine()函数中{}格式项是这样规定的:
格式项都采用如下形式:
{index[,alignment][:formatString]}

其中"index"指索引占位符,这个肯定都知道;
",alignment"按字面意思显然是对齐方式,以","为标记;
":formatString"就是对输出格式的限定,以":"为标记。

alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。

formatString:由标准或自定义格式说明符组成。

由上述说明可知,你的要求写法了:
double x;
x = Convert.ToDouble(Console.ReadLine());
int d=3;
string fat = "{0:f" + d.ToString() + "}";
Console.WriteLine(fat, x);
Console.ReadLine();

热心网友 时间:1小时前

static void Main(string[] args)
        {
            double x;
            x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入小数位数");
            string y = Console.ReadLine();
            string f = "0:f" + y.ToString(); ;
            Console.WriteLine("{"+ f + "}", x);
            Console.ReadLine();
        }

热心网友 时间:1小时前

using System;
using System.Collections.Generic;
using System.Text;
 
namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string format = "{0:f" + n.ToString() + "}";
            double x = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine(format, x);
        }
    }
}

 

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com