Rounding down to 2 decimal places in c#

前端 未结 7 2241
渐次进展
渐次进展 2020-12-03 10:07

How can I multiply two decimals and round the result down to 2 decimal places?

For example if the equation is 41.75 x 0.1 the result will be 4.175. If I do this in c

7条回答
  •  一生所求
    2020-12-03 10:11

    This is my Float-Proof Round Down.

        public static class MyMath
    {
        public static double RoundDown(double number, int decimalPlaces)
        {
            string pr = number.ToString();
            string[] parts = pr.Split('.');
            char[] decparts = parts[1].ToCharArray();
            parts[1] = "";
            for (int i = 0; i < decimalPlaces; i++)
            {
                parts[1] += decparts[i];
            }
            pr = string.Join(".", parts);
            return Convert.ToDouble(pr);
        }
    }
    

提交回复
热议问题