Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converter into euro c#

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.

like image 616
Nelly Avatar asked Dec 11 '22 20:12

Nelly


2 Answers

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 €)

like image 156
Alex Avatar answered Dec 25 '22 05:12

Alex


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  
like image 30
Noctis Avatar answered Dec 25 '22 03:12

Noctis