Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency formatting

Tags:

This should be an easy problem but...

I need to format a currency for display (string) in C#

The currency in question will have its own rules such as the symbol to use and if that symbol should come before the value (e.g. $ 10) or after (e.g. 10 ₫ which is Vietnamese Dong).

But how the numbers are formatted depends upon the users local, not the currency.

E.g.

1.234,56 ₫ should be displayed to a user in Vietnam but  1,234.56 ₫ should be displayed to a user in the US 

(formatted as code so easy to see difference between , and.)

So code like

Double vietnamTotal = 1234.56; return vietnamTotal.ToString("c");   

Won't work as it will use the users (or more accuratly CultureInfo.CurrentCulture) locale for format and currency so you would get things like $1,123.56 - right use of , and . but wrong symbol.

Double vietnamTotal = 1234.56; CultureInfo ci = new CultureInfo(1066); // Vietnam return vietnameTotal.ToString("c",ci));  

Would give 1.234,56 ₫ - Right symbol, wrong use of , and . for current user.

This post gives more detail on the right thing to do, but not how to do it.

What obvious method hidden in the framework am I missing?

like image 683
Ryan Avatar asked Jul 01 '09 20:07

Ryan


People also ask

How do you represent currency?

If the currency type is in question, the symbol may include a country prefix, such as CA$, Can$ or C$ for Canadian dollars, or US$ for U.S. dollars. The € symbol is used to represent the euro currency, ¥ represents the Japanese yen, and ƒ signifies a florin, such as the Aruban Florin.

Is there a space between USD and Number?

The answer is, no, there is no space between the dollar symbol and the amount for US currency.

How do I format text to currency in Excel?

Generally, you should use the Format Cells dialog (Ctrl+1) or Home > Number > Accounting Number Format option to apply a currency formatting to a cell.


2 Answers

  • Take the NumberFormatInfo from the user's currency, and clone it
  • Set the CurrencySymbol in the cloned format to the CurrencySymbol of the currency in question
  • If you want the currency position (and some other aspects of the format) to be copied, set CurrencyPositivePattern and CurrencyNegativePattern in the same way.
  • Use the result to format.

For example:

using System; using System.Globalization;  class Test {         static void Main()     {         decimal total = 1234.56m;         CultureInfo vietnam = new CultureInfo(1066);         CultureInfo usa = new CultureInfo("en-US");          NumberFormatInfo nfi = usa.NumberFormat;         nfi = (NumberFormatInfo) nfi.Clone();         NumberFormatInfo vnfi = vietnam.NumberFormat;         nfi.CurrencySymbol = vnfi.CurrencySymbol;         nfi.CurrencyNegativePattern = vnfi.CurrencyNegativePattern;         nfi.CurrencyPositivePattern = vnfi.CurrencyPositivePattern;          Console.WriteLine(total.ToString("c", nfi));     } } 

Admittedly my console doesn't manage to display the right symbol, but I'm sure that's just due to font issues :)

like image 92
Jon Skeet Avatar answered Oct 13 '22 22:10

Jon Skeet


Sorry, I may be being a little slow here, but I still don't see your point. It seems to me that the original question is "I have a dong value which, in Vietnam, I want displayed as per Vietnamese currency format, with a "₫" symbol, and in the US, I want displayed as per US currency format, but still with a "₫".".

I guess I'm getting confused by the two contradictory statements... "The currency in question will have its own rules" and "how the numbers are formatted depends upon the users local [sic], not the currency."

If all you wanted to change was the currency symbol, and leave the formatting as per the current culture, wouldn't cloning the nfi and setting the symbol be enough ?

     NumberFormatInfo nfi;       // pretend we're in Vietnam      Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("vi-VN");       nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;      nfi.CurrencySymbol = "₫";       String s1 = (1234.5678).ToString("c", nfi);       // pretend we're in America      Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");       nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;      nfi.CurrencySymbol = "₫";       String s2 = (1234.5678).ToString("c", nfi); 
like image 31
Black Light Avatar answered Oct 13 '22 21:10

Black Light