.net - Format currency without rounding -
I need to format a decimal number in the form of currency, but for any rounds in the process Do not want. / P>
For example (example culture is N-US)
slow money = 1234.556789 D money. Towestring ("C") 'yields $ 1,234.56 (goling and layoff notice) money. Toastring ("C 99") 'Yield $ 1,234.556789000000000000 .... 0 - Near but not complete, those indexed zeros are unwanted.' I want something that should be the result ... money. Toastring ("??") 'yields $ 1,234.56789' Similarly .... Money = 0.1D Money Toastring ("??") 'yields $ 0.10 (note that this corresponds to the currency format of the culture at least - two decimal places)
Value to all users of this application Take that "I" ??? "############################################################################################ ######################### "- But there is a problem in my stomach to complete the underlying What's the way I do?
You can do something like this ...
var money = 1234.556789 D; Console.WriteLine (money.ToString (GetFormat (Money)); Money = 1d; Console.WriteLine (money.ToString (GetFormat (Money));
To get the format string, use the following method ...
Fixed String GetFormat (double input) {// Get the number of decimal places Show int length = input.ToString () Length - Input ToString (). IndexOf (".") - 1; Return to the currency format string to use with / decimal string. Toastring () return string. Format ("C {0}", Length <2? 2: Length); }
If you want to take it one step further, you can also wrap it all in one extension method so that you need to call the GetStorm () method from within ToString Do not be. () - which can see things a bit cleaner.
Comments
Post a Comment