Reputation: 7336
I've a console application with the following:
var value = 999999;
System.Console.WriteLine("Price {0:c}", value);
// Prints: Price ? 999.999,00
// Expecting: Price € 999.999,00
The line below prints Price $ 999.999,00
System.Console.WriteLine("Price {0}", value.ToString("c", new CultureInfo("en-US")));
But this line:
System.Console.WriteLine("Price {0}", value.ToString("c", new CultureInfo("fr-BE")));
Prints ? instead of €
What's wrong with the code?
Upvotes: 2
Views: 5882
Reputation: 31270
There is nothing wrong in the code assuming your default culture is supposed to print euro. However you seems to be running the program in a console window either directly or through VS launch. The default code page that is used is 437 that can't display the euro symbol. You have to change it to one that can, for example 1252.
C:\>chcp
Active code page: 437
C:\>chcp 1252
Active code page: 1252
C:\>\Full\Path\To\Your\Program.Exe
€ 9.999,00
Now if you run your program you should see something different. If you are using the default font (Raster Fonts) it would still not display the euro symbol correctly. Change it to Lucida Console and the program should work as expected.
If you want to control this from the program, add appropriate encoding
//Console.OutputEncoding = Encoding.Default;
//Console.OutputEncoding = Encoding.UTF8;
Upvotes: 4
Reputation: 2575
According to this answer you could modify your code as follows:
Console.WriteLine("Price: " + value.ToString("C", CultureInfo.CreateSpecificCulture("en-US")));
Add your CultureInfo accordingly.
Upvotes: 0
Reputation: 9314
First,
var int value = 999999;
isn't right.
it can be either
var value = 999999;
or
int value = 999999;
Second, Whether or not it prints € or $ or some other currency symbol will depend on your current culture.
e.g.
using System;
public class Program
{
public static void Main()
{
var value = 999999;
System.Console.WriteLine("Price {0:c}", value);
}
}
Gives me the output
Price $999,999.00
To specify the currency symbol, we can also do the following
var numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
numberFormatInfo = (NumberFormatInfo) numberFormatInfo.Clone();
numberFormatInfo.CurrencySymbol = "€";
Console.WriteLine(string.Format(numberFormatInfo, "{0:c}", value));
Upvotes: 0
Reputation: 11570
Use decimal instead of int.
public class TestDecimalFormat
{
static void Main ()
{
decimal x = 0.999m;
decimal y = 9999999999999999999999999999m;
Console.WriteLine("My amount = {0:C}", x);
Console.WriteLine("Your amount = {0:C}", y);
}
}
Upvotes: 0