标签: string-formatting

使用string.Format("{0:00}"格式化字符串

我刚刚接过一些代码,我看到这个用了很多.它似乎取整数并创建一个看起来像"01","02"等的字符串.

我不确定的是这里使用的惯例.为什么格式{0:00}而不是{00}

string.Format("{0:00}", int.Parse(testVal) + 1);
Run Code Online (Sandbox Code Playgroud)

.net c# string .net-4.0 string-formatting

23
推荐指数
1
解决办法
7万
查看次数

Html.DisplayFor十进制格式?

我有一个小数值,例如: 59625879,00

我想像这样显示这个值:59.625,879或者59625,879

我怎么能这样做@Html.DisplayFor(x => x.TAll, String.Format())

谢谢.

asp.net-mvc string-formatting

23
推荐指数
1
解决办法
6万
查看次数

如何在String.Format中放入未处理(转义)的单词

我正在格式化日期:

str = String.Format("{0:MMM d m:mm"+yearStr+"}", dt);
Run Code Online (Sandbox Code Playgroud)

我想在"d"之后加上"at"这个词,但我不希望字符串格式化它.我只想要"at"这个词.

我怎样才能做到这一点?

.net c# escaping date string-formatting

23
推荐指数
2
解决办法
1万
查看次数

Numbers.js是否有格式,仅在需要时显示小数?

测试:http://jsfiddle.net/su918rLv/

这是现有格式的工作方式:

numeral(1234.567).format('0,0');
//output: 1,235

numeral(1234.567).format('0,0.00');
//output: 1,234.57    

numeral(1234).format('0,0.00');
//output: 1,234.00
Run Code Online (Sandbox Code Playgroud)

是否有一种格式可以根据数值生成整数或十进制数?我在0,0.99这里使用但不是答案.

numeral(1234).format('0,0.99');
//output: 1,234

numeral(1234.567).format('0,0.99');
//output: 1,234.57
Run Code Online (Sandbox Code Playgroud)

javascript string-formatting numeral.js

23
推荐指数
2
解决办法
1万
查看次数

如何在没有百分号的情况下将数字格式化为百分比?

如何在.NET中将数字格式化为百分比而不显示百分号?

如果我有数字0.13并使用格式字符串{0:P0}输出13 %.

但是我想13改为,而不必将数字乘以100并使用格式字符串{0:N0}.

(背景:在ASP.NET中,我有一个带有BoundField的GridView,我想在其中显示一个数字,但不显示百分号(%).我该怎么做?)


谢谢你的回答.在编辑时,如上所述,在6个中有4个表示我想要避免的内容.我一直在寻找一种只使用格式字符串的方法,并避免乘以100并使用{0:N0},但答案表明这是不可能的......


使用理查德接受的解决方案解决:


public class MyCulture : CultureInfo
{
    public MyCulture()
        : base(Thread.CurrentThread.CurrentCulture.Name)
    {
        this.NumberFormat.PercentSymbol = "";
    }
}

Thread.CurrentThread.CurrentCulture = new MyCulture();
Run Code Online (Sandbox Code Playgroud)

c# string-formatting

22
推荐指数
3
解决办法
3万
查看次数

string.Format在运行时因整数数组而失败

考虑string.Format()哪些参数是字符串,以及在重载列表中的一个object[]或多个对象.

该声明成功:

string foo = string.Format("{0} {1}", 5, 6);
Run Code Online (Sandbox Code Playgroud)

就像这样:

object[] myObjs = new object[] {8,9};
string baz = string.Format("{0} and {1}", myObjs;
Run Code Online (Sandbox Code Playgroud)

和一串字符串一样:

string[] myStrings = new string[] {"abc", "xyz"};
string baz = string.Format("{0} {1}", myStrings);
Run Code Online (Sandbox Code Playgroud)

似乎整数,当单独指定时,可以加框或强制键入object,而后者又被强制为字符串.

此语句在运行时失败.

int[] myInts = new int[] {8,9};
string bar = string.Format("{0} and {1}", myInts);
Run Code Online (Sandbox Code Playgroud)

索引(从零开始)必须大于或等于零且小于参数列表的大小.

  • 为什么不强制或不能将int数组强制或加入一个object[]string[]
  • 出于一点点好奇心,为什么编译器没有抓住这个?

c# arrays string-formatting

22
推荐指数
1
解决办法
5618
查看次数

字符串格式选项:优点和缺点

这是在Python中格式化字符串的两种非常流行的方法.一个是使用dict:

>>> 'I will be %(years)i on %(month)s %(day)i' % {'years': 21, 'month': 'January', 'day': 23}
'I will be 21 on January 23'
Run Code Online (Sandbox Code Playgroud)

而另一个使用简单tuple:

>>> 'I will be %i on %s %i' % (21, 'January', 23)
'I will be 21 on January 23'
Run Code Online (Sandbox Code Playgroud)

第一个是更具可读性,但第二个更快写.我实际上模糊地使用它们.

每个人的利弊是什么?关于性能,可读性,代码优化(其中一个转换为另一个?)以及您认为有用的任何其他内容.

python string-formatting

22
推荐指数
2
解决办法
1602
查看次数

Combobox Displaymemberpath上的StringFormat

我试图在ComboBox(WPF)的DisplayMemberPath属性上使用StringFormat.但即使这是可能的,我也不知道.有人可以帮助我一些想法.

我想做这样的事情:

  <ComboBox DisplayMemberPath="{Binding Path=MyDateField, StringFormat={}{0:dd/MM/yyyy}}" Name="CmbName" Width="120" />
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

大家好

wpf combobox string-formatting

22
推荐指数
1
解决办法
2万
查看次数

我应该如何在C++的类方法中正确使用__attribute __((format(printf,x,y)))?

我正在尝试为调试打印定义一个类方法,其行为类似于printf:

inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)))
Run Code Online (Sandbox Code Playgroud)

这抱怨:

error: format string argument not a string type
Run Code Online (Sandbox Code Playgroud)

我记得类方法声明有一个隐式this参数,所以我将参数的位置更改为2,3:

inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 2, 3)))
Run Code Online (Sandbox Code Playgroud)

现在它编译,但看起来像参数被移位,就好像this参数被视为参数列表的一部分.

如何判断this不属于我要打印的字符串的函数?

c++ printf gcc string-formatting

22
推荐指数
3
解决办法
2万
查看次数

使用格式说明符的python中心字符串

我有一个名为Message的字符串.

Message = "Hello, welcome!\nThis is some text that should be centered!"
Run Code Online (Sandbox Code Playgroud)

是的,这只是一个测试声明......

我试图将它作为一个默认的终端窗口,即宽度为80,并使用以下语句:

print('{:^80}'.format(Message))
Run Code Online (Sandbox Code Playgroud)

哪个印刷品:

           Hello, welcome!
This is some text that should be centered!           
Run Code Online (Sandbox Code Playgroud)

我期待的是:

                                Hello, welcome!                                 
                   This is some text that should be centered!                   
Run Code Online (Sandbox Code Playgroud)

有什么建议?

python formatting string-formatting python-3.x python-3.2

22
推荐指数
1
解决办法
2万
查看次数