C# 需要自定义货币格式与 String.Format 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542690/
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
Need a custom currency format to use with String.Format
提问by Notorious2tall
I'm trying to use String.Format("{0:c}", somevalue) in C# but am having a hard time figuring out how to configure the output to meet my needs. Here are my needs:
我正在尝试在 C# 中使用 String.Format("{0:c}", somevalue) 但我很难弄清楚如何配置输出以满足我的需求。以下是我的需求:
- 0 outputs to blank
- 1.00 outputs to $1.00
- 10.00 outputs to $10.00
- 100.00 outputs to $100.00
- 1000.00 outputs to $1,000.00
- 0 输出到空白
- 1.00 输出到 1.00 美元
- 10.00 输出到 10.00 美元
- 100.00 输出到 100.00 美元
- 1000.00 输出到 1,000.00 美元
I've tried String.Format("{0:c}", somevalue) but for zero values it outputs $0.00 which is not what I want. I've also tried String.Format("{0:$0,0.00;$(0,0.00);#}", somevalue), but for 1.0 it outputs $01.00. String.Format("{0:$0.00;$(0.00);#}", somevalue) works for most cases, but when somevalue is 1000.00 the output is $1000.00.
我试过 String.Format("{0:c}", somevalue) 但是对于零值,它输出 $0.00 这不是我想要的。我也试过 String.Format("{0:$0,0.00;$(0,0.00);#}", somevalue),但对于 1.0,它输出 $01.00。String.Format("{0:$0.00;$(0.00);#}", somevalue) 适用于大多数情况,但当 somevalue 为 1000.00 时,输出为 $1000.00。
Is there some format that will fit all 5 cases above? All of the documentation I've read only details the basics and doesn't touch on this type of scenario.
是否有适合上述所有 5 种情况的格式?我读过的所有文档都只详细介绍了基础知识,并没有涉及这种类型的场景。
采纳答案by DavGarcia
If you use
如果你使用
string.Format("{0:$#,##0.00;($#,##0.00);''}", value)
You will get "" for the zero value and the other values should be formatted properly too.
您将获得零值的 "",其他值也应正确格式化。
回答by Andrew Hare
Try something like this:
尝试这样的事情:
String currency = (number == 0) ? String.Empty : number.ToString("c");
回答by scwagner
Depending on if you are consistently using the same data type for all of your currency values, you could write an extension method that would make it so that your case is always met. For example if you were using the decimal type:
根据您是否始终为所有货币值使用相同的数据类型,您可以编写一个扩展方法,使其始终满足您的情况。例如,如果您使用的是小数类型:
public static string ToCurrencyString (this decimal value)
{
if (value == 0)
return String.Empty;
return value.ToString ("C");
}
回答by juan
Here'sa great reference that you might find useful, which summarises this data: http://blog.stevex.net/string-formatting-in-csharp/
这里有一个很好的参考,你可能会觉得有用,它总结了这些数据:http: //blog.stevex.net/string-formatting-in-csharp/
回答by MEC
The "C" currency formats are great until you need a blank for 0. Here are two ways, one mentioned above, similar to the ones that I use that give you the blank for 0:
“C”货币格式很好,直到你需要一个空白的 0。这里有两种方法,上面提到的一种,类似于我使用的那些给你 0 的空白:
// one way
string.Format("{0:$#,##0.00;($#,##0.00);''}", somevalue)
// another way
somevalue.ToString("$#,##0.00;($#,##0.00);''")
The second technique feels more "fluent", if you like that style of code (as I do).
第二种技术感觉更“流畅”,如果你喜欢那种风格的代码(就像我一样)。