Reputation: 5670
I have a price string which contains a data like this
22,95
This price is in danish format, integers seperated by comma. I want to convert this in to universal format. means I want an out put like this
22.95
I have tried some thing like this
public static string getFormattedPrice(string value)
{
double moneyValue;
System.Globalization.CultureInfo english = new System.Globalization.CultureInfo("en-US");
Double.TryParse(value, System.Globalization.NumberStyles.AllowDecimalPoint, english, out moneyValue);
return value;
}
But the result is still
22,95
Any one know what is the correct way to implement this. Note:please don't tell me I can make string replace, where all comma can be replaced with a dot.As this is a business critical data and I want to go with correct way.
Upvotes: 1
Views: 170
Reputation: 38683
Try this simple way
decimal dollarAmount = 123165.4539M;
string text = dollarAmount.ToString("C",Cultures.US);
Console.WriteLine(text);
public static class Cultures
{
public static readonly CultureInfo US=
CultureInfo.ReadOnly(new CultureInfo("en-US"));
}
Upvotes: 1
Reputation: 1498
try below code
public static string getFormattedPrice(string value)
{
double moneyValue ;
System.Globalization.CultureInfo english = new System.Globalization.CultureInfo("en-US");
Double.TryParse(value, System.Globalization.NumberStyles.AllowDecimalPoint, english, out moneyValue);
return moneyValue.toString();
}
Upvotes: 0
Reputation: 125650
First of all, you should use decimal
instead of double
to represent financial data.
Then, you're trying to parse your string using en-GB
format, but as your said it's da-DK
. Instead of parsing data using English culture info you should use it after parsing, to get new string.
And what even more important, you're returning exactly the same value that was passes as method parameter. Why do you expect it to work?
This one works as expected:
public static string getFormattedPrice(string value)
{
decimal moneyValue;
var danish = new System.Globalization.CultureInfo("da-DK");
decimal.TryParse(value, System.Globalization.NumberStyles.AllowDecimalPoint, danish, out moneyValue);
return moneyValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
}
Upvotes: 3