String in currency format, without periods (or commas)

set value = 2 or 4.00
outputs below

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

value = $ 2.00, or $ 4.00

if I have value = 1000 then the output will be $1,000.00, but I need $1000.00.

I do not prefer the concatenation of the strings "$" and the value.

+5
source share
4 answers
var stringValue = Convert.ToDecimal(value).ToString("$0.00");

@James , . C2 . (, Windows 7 - - - - - - ) C2 .

@James . NumberFormat, CurrencyGroupSeparator.

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

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

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));

, .

+4
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);`
+2
source

Convert integer value to $ 0.00 format

int Value = 1000; string abc = Convert.ToDecimal (Value) .ToString ("$ 0.00"); yield will be $ 1000.00

0
source

All Articles