How to parse string to decimal with currency symbo

2019-01-27 13:18发布

问题:

I have no idea why this is not working:

string s = "12,00 €";
var germanCulture = CultureInfo.CreateSpecificCulture("de-DE");
decimal d;
if (decimal.TryParse(s, NumberStyles.AllowCurrencySymbol, germanCulture, out d))
{
    // i want to get to this point
    Console.WriteLine("Decimal value: {0}", d);
}

回答1:

Use NumberStyles.Currency instead of NumberStyles.AllowCurrencySymbol

if (decimal.TryParse(s, NumberStyles.Currency, germanCulture, out d))

and the output for you code would be:

Decimal value: 12.00


回答2:

Try this;

 string.Format(new CultureInfo("en-SG", false), "{0:c0}", 123423.083234);

It will convert 123423.083234 to $1,23,423 format.