2

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?

Stacked
  • 6,892
  • 7
  • 57
  • 73

4 Answers4

4

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.

Command Prompt  Menu Command Prompt Properties

If you want to control this from the program, add appropriate encoding

//Console.OutputEncoding = Encoding.Default;
//Console.OutputEncoding = Encoding.UTF8;
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Active code page: 850, changed the font to Lucida Console but still not working – Stacked Nov 11 '14 at 20:38
  • @Stacked, start with 1252 not 850. – amit_g Nov 11 '14 at 20:45
  • Changing the current OutputEndcoding "Western European (DOS)" to Unicode (UTF-8) solved the issue: System.Console.OutputEncoding = Encoding.UTF8; Thanks amit_g – Stacked Nov 11 '14 at 21:09
  • I just wrote Console.OutputEncoding = Encoding.Default; and Console.OutputEncoding = Encoding.UTF8; in Program.cs file, it works for me. No other things did not require for me. – Abhijit Mondal Abhi Apr 09 '23 at 10:12
0

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);
    }
}
Community
  • 1
  • 1
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
0

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));
ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
0

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.

Community
  • 1
  • 1
Mauricio Arias Olave
  • 2,259
  • 4
  • 25
  • 70