wpf 带逗号的字符串格式整数,无小数位,0 值无
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18844066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Format Integer With Commas, No Decimal Places and Nothing for 0 Values
提问by I am Bish
Okay before I get right into my question does anyone know if there is a tool for building string formats? What I'm thinking is something like a pretty simple user interface where you choose whether you want to display commas, zero values, dollar signs etc and then it spits out the stringformat for you. If there is one I'd love to know about it. If there's not it's the sort of thing people like me would love!
好的,在我开始我的问题之前,有没有人知道是否有用于构建字符串格式的工具?我在想的是一个非常简单的用户界面,您可以在其中选择是否要显示逗号、零值、美元符号等,然后它会为您输出字符串格式。如果有的话,我很想知道。如果没有,那是像我这样的人会喜欢的东西!
My question is. What is the string format for displaying an integer with comma separators for thousands, no decimal places and nothing for zero values.
我的问题是。显示带有千位逗号分隔符的整数的字符串格式是什么,没有小数位,零值没有任何内容。
I know the string format for an integer with comma separators and no decimal places is:
我知道带有逗号分隔符且没有小数位的整数的字符串格式是:
StringFormat='0,0.'
And I know the string format for displaying nothing for zero values is:
而且我知道零值不显示任何内容的字符串格式是:
StringFormat='{}{0:#}'
But combining the two has me stumped. Thanks very much!
但是将两者结合起来让我很难过。非常感谢!
回答by Sheridan
I'm not 100% sure what you're after, but after comparing your two string formats, I thinkI know what you're after... please let me know if I am mistaken.
我不是 100% 确定你在追求什么,但在比较你的两种字符串格式后,我想我知道你在追求什么......如果我弄错了,请告诉我。
Once again, you almosthad what I thinkyou want... how about trying this:
再一次,你几乎得到了我认为你想要的东西......试试这个怎么样:
StringFormat='{}{0:#,#.}'
Or just
要不就
StringFormat='#,#.' (Just replace the '0' from your example with '#')
These are equivalent. Please note that again, these will both round the number to the nearest integer.
这些是等价的。请再次注意,这些都会将数字四舍五入到最接近的整数。
UPDATE >>>
更新 >>>
Here are two very useful links to help you with your string.Format
s in the future:
这里有两个非常有用的链接,可以帮助您string.Format
将来处理您的s: