In the project, you use the truncation method to take two digits after the decimal point, so the following methods are written:
Copy Code code as follows:
<summary>
Truncates a decimal value by the specified number of decimal places
</summary>
<param name= "D" > Decimal number to truncate </param>
<param name= "s" > decimal digits, s greater than or equal to 0, less than or equal to 28</param>
<returns></returns>
public static decimal ToFixed (decimal d, int s)
{
Decimal sp = Convert.todecimal (Math.pow (s));
if (d < 0)
Return Math.truncate (d) + math.ceiling ((d-math.truncate (d)) * sp)/SP;
Else
Return Math.truncate (d) + Math.floor ((d-math.truncate (d)) * sp)/SP;
}
<summary>
Truncates a double-precision floating-point value by the specified number of decimal places
</summary>
<param name= "D" > Double precision floating-point number to truncate </param>
<param name= "s" > decimal digits, s greater than or equal to 0, less than or equal to 15</param>
<returns></returns>
public static double ToFixed (double d, int s)
{
Double sp = Math.pow (s);
if (d < 0)
Return Math.truncate (d) + math.ceiling ((d-math.truncate (d)) * sp)/SP;
Else
Return Math.truncate (d) + Math.floor ((d-math.truncate (d)) * sp)/SP;
}
Incidentally mention:
The ToString ("#.##") method of double and decimal uses rounding;
Static class System.Math under the round (decimal d, int decimals) method, rounding the way used is "four homes six into 50% pairs";
The third parameter of the round (decimal d, int decimals, midpointrounding mode) under Static Class System.Math is an enumeration parameter that indicates how to handle the intermediate value (5);
Static Class System.Math Method: Http://msdn.microsoft.com/zh-cn/library/system.math_methods (v=vs.80)