简体   繁体   中英

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.

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

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM