4

I have to convert from Lei into euro by using string formatting for currency. My method is:

public static void ConvertFromRonEur()
    {
        //string amount = string.Format("{0:C}");
        double result;
        Console.WriteLine("Lei: ");
        double quantity;
        double euro = 0.22D;
        quantity = double.Parse(Console.ReadLine());
        result = quantity * euro;
        Console.WriteLine(("{0:C} Euro"), result);
    }

When I run the result is:

  Lei:
  10
  $2,20 Euro

How can I have only the 2,20 Euro result, but to use the string formatting currency? Thank you.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
Nelly
  • 299
  • 3
  • 5
  • 16

2 Answers2

12

You need to specify the Culture, to a String.Format

Something like

//use any european culture
var cultureInfo = CultureInfo.GetCultureInfo("fr-FR"); 
Console.WriteLine(String.Format(cultureInfo, "{0:C} Euro", result));

An alternative

Console.WriteLine(string.Format("€{0:N2} Euro", result));

Format to 2 decimal places (prefixed by €)

Alex
  • 37,502
  • 51
  • 204
  • 332
2

From: MSDN:

"C" or "c" : Currency Result: A currency value. Supported by: All numeric types. Precision specifier: Number of decimal digits. Default precision specifier: Defined by NumberFormatInfo.CurrencyDecimalDigits.

More information: The Currency ("C") Format Specifier.

123.456 ("C", en-US) -> $123.46  
123.456 ("C", fr-FR) -> 123,46 €  
123.456 ("C", ja-JP) -> ¥123  
-123.456 ("C3", en-US) -> ($123.456)  
-123.456 ("C3", fr-FR) -> -123,456 €  
-123.456 ("C3", ja-JP) -> -¥123.456  
Noctis
  • 11,507
  • 3
  • 43
  • 82