需要自定义货币格式才能与String.Format一起使用 [英] Need a custom currency format to use with String.Format
问题描述
我正在尝试在C#中使用String.Format( {0:c},somevalue),但是很难弄清楚如何配置输出以满足我的需求。这是我的需求:
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:
- 0输出为空白
- 1.00输出为$ 1.00
- 10.00输出至$ 10.00
- 100.00输出至$ 100.00
- 1000.00输出至$ 1,000.00
- 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
我尝试过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。
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.
是否有某种格式可以满足以上所有5种情况?我仅阅读过的所有文档都详细介绍了基础知识,并且不涉及此类情况。
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.
推荐答案
如果使用
string.Format("{0:$#,##0.00;($#,##0.00);''}", value)
您将获得作为零值,其他值应为格式也正确。
You will get "" for the zero value and the other values should be formatted properly too.
这篇关于需要自定义货币格式才能与String.Format一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!