格式化双值,如货币但没有货币符号(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1048643/
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
Format a double value like currency but without the currency sign (C#)
提问by Houman
I feed a textbox a string value showing me a balance that need to be formatted like this:
我给一个文本框输入一个字符串值,显示一个需要像这样格式化的余额:
###,###,###,##0.00
I could use the value.ToString("c"), but this would put the currency sign in front of it.
我可以使用 value.ToString("c"),但这会将货币符号放在它前面。
Any idea how I would manipulate the string before feeding the textbox to achieve the above formatting?
知道如何在输入文本框之前操作字符串以实现上述格式吗?
I tried this, without success:
我试过这个,没有成功:
String.Format("###,###,###,##0.00", currentBalance);
Many Thanks,
非常感谢,
采纳答案by LukeH
string forDisplay = currentBalance.ToString("N2");
回答by Jon Skeet
If the currency formatting gives you exactly what you want, clone a NumberFormatInfowith and set the CurrencySymbolproperty to "". You should check that it handles negative numbers in the way that you want as well, of course.
如果货币格式完全符合您的要求,请克隆 aNumberFormatInfo并将CurrencySymbol属性设置为“”。当然,您应该检查它是否也以您想要的方式处理负数。
For example:
例如:
using System;
using System.Globalization;
class Test
{
static void Main()
{
NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
nfi = (NumberFormatInfo) nfi.Clone();
Console.WriteLine(string.Format(nfi, "{0:c}", 123.45m));
nfi.CurrencySymbol = "";
Console.WriteLine(string.Format(nfi, "{0:c}", 123.45m));
}
}
The other option is to use a custom numeric format string of course - it depends whether you really want to mirror exactly how a currency would look, just without the symbol, or control the exact positioning of digits.
当然,另一种选择是使用自定义数字格式字符串 - 这取决于您是否真的想要准确地反映货币的外观,只是没有符号,或者控制数字的确切位置。
回答by Garry Shutler
Have you tried:
你有没有尝试过:
currentBalance.ToString("#,##0.00");
This is the long-hand equivalent of:
这是长手等价物:
currentBalance.ToString("N2");
回答by Prasanna
CultureInfo cultureInfo = new CultureInfo("en-US");
cultureInfo.NumberFormat.CurrencySymbol = "Rs.";
Thread.CurrentThread.CurrentCulture = cultureInfo;
decimal devimalValue = 3.45M;
this.Text = devimalValue.ToString("C2"); //Rs.3.45
回答by webKite
string result=string.Format("{0:N2}", value); //For result like ### ### ##.##
string result=string.Format("{0:N2}", value); //对于像### ### ##.## 这样的结果
回答by Jordan Parker
You can do this with the group separatorand the section separator, like this:
currentBalance.ToString("#,0.00;(#,0.00)");
This does not account for culture variances like the answer from @JonSkeet would, but this does mimic decimal place, rounding, thousands separation, and negative number handling that en-US culture currency format produces using a single custom format string.
这并没有像@JonSkeet 的答案那样考虑文化差异,但这确实模拟了使用单个自定义格式字符串生成的 en-US 文化货币格式所产生的小数位、四舍五入、千位分隔和负数处理。
回答by HerGiz
This may be overkill, but it rounds, formats...
这可能有点矫枉过正,但它是圆形的,格式......
@helper TwoDecimalPlaces(decimal? val)
{
decimal x = 0;
decimal y = 0;
string clas = "text-danger";
if (val.HasValue)
{
x = (decimal)val;
if (val > 0)
{
clas = "";
}
}
y = System.Math.Round(x, 2);
IFormatProvider formatProvider = new System.Globalization.CultureInfo(string.Empty);
<span class="@clas">@string.Format("{0:N2}", y)</span>
}
回答by Taylor Brown
This simple solution works for me with US currency.
这个简单的解决方案适用于美国货币。
If not needing international currency support use this and replace the $ with the currency symbol(s) to be removed:
如果不需要国际货币支持,请使用此选项并将 $ 替换为要删除的货币符号:
// for USD
string result = currentBalance.ToString("C").Replace("$", "")
or
或者
// for EUR
string result = currentBalance.ToString("C").Replace("", "")

