码迷,mamicode.com
首页 > 其他好文 > 详细

课后作业 06 -- 小数后几位精确判断并输出

时间:2015-08-08 01:19:19      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

            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();

  

课后作业 06 -- 小数后几位精确判断并输出

标签:

原文地址:http://www.cnblogs.com/whytohow/p/4712327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!