views:

46

answers:

3

Hello,

In my application a calculation is performed which display the text to the GUI. The application multiplies a user given amount by a defined number (Say 0.85) to create a total (User types in 2, application works out 2 x 0.85).

As the number that is displayed is that of a currency I am trying to correctly format the text to make it readable and correct.

So far I have tried

.ToString("N2");

This has just resulted in two additional zero's being appended to the end of the figure.

The problem can be seen here:

Error!

As you can see the correct value is .68 (Or £0.68) and my text is showing £68.00 Taking the "N2" out of the ToString does help but I'm still left with £68.

I know this is not as trivial as it sounds but it's something I've never needed to think about before and it's got me thinking about it for a long while.

Thanks!

Note: The data is stored as a double and was previously a float, the application is flexible to change. The currency icon is also not needed as I am providing that manually, only the formatting is necessary.

+2  A: 

Try this:

string.Format("{0:C}", money_value);

This will also work:

.ToString("C");

(I realize this will include the currency symbol, but the OP didn't say that was a problem, just that it wasn't necessary.)

If you want to go all out, you can do this:

string.Format(ui_culture, “{0:C}”, money_value);

where ui_culture is the culture associated with the currency.

Edited to add:

The good thing about this formatting is that it manages all your punctuation.

I'm not sure if currency symbols are always the leading character. If so, you can remove it:

string.Format(ui_culture, “{0:C}”, money_value).substring(1);
egrunin
That will include the currency symbol - http://msdn.microsoft.com/en-us/library/dwhawy9k(VS.71).aspx - but from the screen shot that's not needed as it's already present as a label.
ChrisF
I have up voted the answer as it was very helpful but another answer managed to fix it. Also if my application didn't have the need for the drop down box and was reliant on the users defined language then your solution would definitely be the chosen one.
Jamie Keeling
A: 

You would need to use the ToString overload that takes an IFormatProvider paramter:

double value = 80;
string ukCurrency = value.ToString("N2", CultureInfo.CreateSpecificCulture("en-GB"));

I am actually not sure if this will include the currency character (untested example). I am hoping that the use of the "N2" format string will strip the currency character...but it may not. It might be easy enough to skip the first character of the string:

ukCurrency = ukCurrency.Substring(1);
jrista
+1  A: 

At first glance it looks like you've multiplied your value (0.68) by 100 to get 68.00 which would be correct. However, your quantity appears to be 80 which should give you a value of 54.40.

If you are multiplying by 2 then you should get 1.70.

ChrisF
Thank you for the help, after manually working it out it seems I missed out two decimal places into the defined variable. After editing the value the text corrects properly.
Jamie Keeling