I'm not able to convert float to the currency string. I tried to find an answer on the internet.
float price = 29.95f;
Console.WriteLine(price.ToString("C"));
Outputs:
29,95 ?
P.S. Problem is with my console.
I'm not able to convert float to the currency string. I tried to find an answer on the internet.
float price = 29.95f;
Console.WriteLine(price.ToString("C"));
Outputs:
29,95 ?
P.S. Problem is with my console.
As indicated in comments by Lasse, you may be missing a culture:
Try:
Console.WriteLine(price.ToString("C", CultureInfo.CurrentCulture));
For a specific culture, you can try:
price.ToString("C", new CultureInfo("en-GB")); // for uk culture..
decimal is usually the recommendation for currency.
See more here:
You should use decimal
instead of float
for currency because it is more accurate. Anyway your code is fine and it must be a display error.
// Declare price as a decimal.
decimal price = 29.95m;
You can also specify cultures using:
// For German culture.
Console.WriteLine(price.ToString("C", new CultureInfo("en-DE")));
// Output
// 29,95 €
// For British culture.
Console.WriteLine(price.ToString("C", new CultureInfo("en-GB")));
// Ouput
// £29.95
// For American culture.
Console.WriteLine(price.ToString("C", new CultureInfo("en-US")));
// Output
// $29.95
Edit: Just saw your update, glad it was just a display issue. Also, I think you should give this a read. It is better to accept an answer (even if you have to leave your own) rather than edit your post.