Console.Write("输入您要精确的小数:");
            string strA = Console.ReadLine();
            Console.Write("您要精确到几位小数:");
            int k = int.Parse(Console.ReadLine());
            double n = Convert.ToDouble(strA);
            int i = strA.IndexOf(".");//确定小数索引位置
            string strB = string.Empty;
            try
            {
                strB = strA.Substring(i + k + 1, 1);//截取精确小数的后一位

                int m = int.Parse(strB);
                string strC = strA.Substring(i + 1, k);//截取要保留的小数部分

                double j = Math.Floor(n);
                strA = Convert.ToString(j);//提取其整数部分

                strA = strA + "." + strC;//整数加要精确的小数部分
                double q = Convert.ToDouble(strA);
                Console.Write("精确后的数为:");
                if (m >= 5)
                    Console.Write(q + Math.Pow(0.1, k));//五入
                else
                    Console.Write(q);
            }
            catch
            {
                Console.WriteLine("精确的小数位要大于您输入数的小数数!");
            }
            Console.ReadLine();

  

相关文章:

  • 2022-03-01
  • 2021-11-27
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-09-19
猜你喜欢
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
  • 2021-12-23
  • 2021-06-10
  • 2021-07-11
  • 2022-12-23
相关资源
相似解决方案