. Net decimal retains the specified decimal places (not rounded),. netdecimal
Preface
If the project encounters a apportioned amount, the amount in the last line = total amount-sum of the apportioned amount.
This may result in a negative number when the last part is apportioned. Therefore, I wrote a method to retain the decimal digits of the specified number.
The use of extension methods makes calling very elegant.
Sample Code
Public static class DecimalExtension {// <summary> // decimal retains the specified number of digits in decimal places /// </summary> /// <param name = "num"> original quantity </param> /// <param name = "scale"> retain decimal places </param> /// <returns> truncates the number of strings after the specified decimal places </returns> public static string ToString (this decimal num, int scale) {string numToString = num. toString (); int index = numToString. indexOf (". "); int length = numToString. length; if (index! =-1) {return string. format ("{0 }. {1} ", numToString. substring (0, index), numToString. substring (index + 1, Math. min (length-index-1, scale);} else {return num. toString ();}}}
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.