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:
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.
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);
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.
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);
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.