简体   繁体   English

字符串到货币格式,没有句点(或逗号)

[英]string to Currency format, with no periods (or commas)

given value = 2 or 4.00 给定值= 2或4.00
the below statement outputs 以下声明产出

value = Convert.ToDecimal(value).ToString("C2");

value = $2.00, or $4.00 价值= 2.00美元,或4.00美元

if I have value = 1000 then output will be $1,000.00 but I require $1000.00 . 如果我有值= 1000,那么输出将是$1,000.00但我要$1000.00

I don't prefer string concatenation of "$" and value. 我不喜欢“$”和值的字符串连接。

var stringValue = Convert.ToDecimal(value).ToString("$0.00");

As noted by @James below, this hard-codes the currency into the format. 如下面的@James所述,这将货币硬编码为格式。 Using the format C2 will use the system currency format. 使用格式C2将使用系统货币格式。 This can be changed for the system (eg in Windows 7 - Start - Control Panel - Change Display Language - Additional Settings - Currency - Digit grouping) and will allow the C2 format to display the currency value without the comma when running on that particular system. 这可以针对系统进行更改(例如,在Windows 7中 - 开始 - 控制面板 - 更改显示语言 - 其他设置 - 货币 - 数字分组),并允许C2格式在该特定系统上运行时显示没有逗号的货币值。

EDIT 编辑

All credit to @James for using the current culture. 所有归功于@James使用当前文化。 My only modification to his answer would be to clone the current NumberFormat so as to get all the properties of the current culture number format before removing the CurrencyGroupSeparator . 我对他的回答的唯一修改是克隆当前的NumberFormat以便在删除CurrencyGroupSeparator之前获取当前文化编号格式的所有属性。

var formatInfo = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
formatInfo.CurrencyGroupSeparator = string.Empty;

var stringValue = Convert.ToDecimal(value).ToString("C", formatInfo);

You should use the NumberFormat class to specify the type of formatting you need, ToString takes an IFormatProvider parameter eg 您应该使用NumberFormat类来指定所需的格式类型, ToString采用IFormatProvider参数,例如

var formatInfo = (System.Globalization.NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
formatInfo.CurrencyGroupSeparator = ""; // remove the group separator
Console.WriteLine(2.ToString("C", formatInfo));
Console.WriteLine(4.ToString("C", formatInfo));
Console.WriteLine(1000.ToString("C", formatInfo));

This will keep your number formatting consistent with whichever culture you are using. 这将使您的数字格式与您使用的任何文化保持一致。

public static class MyExtensions
{
    public static string GetMoney(this decimal value, bool displayCurrency = false, bool displayPeriods = true)
    {
        string ret = string.Format("{0:C}", value).Substring(displayCurrency ? 0 : 1);
        if (!displayPeriods)
        {
            ret = ret.Replace(",", string.Empty);
        }
        return ret;
    }
}

To use this extension method: 要使用此扩展方法:

decimal test = 40023.2345M;
string myValue = test.GetMoney(displayCurrency:true, displayPeriods:false);`

Converting the integer value into $0.00 format 将整数值转换为$ 0.00格式

int Value = 1000; int值= 1000; string abc = Convert.ToDecimal(Value).ToString("$0.00"); string abc = Convert.ToDecimal(Value).ToString(“$ 0.00”); output will be $1000.00 输出将是$ 1000.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM