using System;
class Test
{
public static void Main()
{
Console.WriteLine("输入一个带一位小数的数字");
string s=Console.ReadLine();
try
{
Double num_s=Double.Parse(s);
//是数字
Console.WriteLine("输入为数字");
if(num_s-(int)num_s>0.5)
Console.WriteLine("四舍五入后结果为{0}",(int)num_s+1);
else
Console.WriteLine("四舍五入后结果为{0}",(int)num_s);
}
catch (System.Exception ex)
{
//不是
Console.WriteLine("输入不是数字{0}",ex);
}
}
}
在输入带小数点的数字时候,总是不能识别出为数字,就算输入整数,四舍五入部分也不执行.
class Test
{
public static void Main()
{
Console.WriteLine("输入一个带一位小数的数字");
string s=Console.ReadLine();
try
{
Double num_s=Double.Parse(s);
//是数字
Console.WriteLine("输入为数字");
if(num_s-(int)num_s>0.5)
Console.WriteLine("四舍五入后结果为{0}",(int)num_s+1);
else
Console.WriteLine("四舍五入后结果为{0}",(int)num_s);
}
catch (System.Exception ex)
{
//不是
Console.WriteLine("输入不是数字{0}",ex);
}
}
}
在输入带小数点的数字时候,总是不能识别出为数字,就算输入整数,四舍五入部分也不执行.
/// 四舍五入测试
/// </summary>
/// <param name="strValue">输入的字符串型数字</param>
private void Test(string strValue)
{
try
{
Double num_s = Double.Parse(strValue); // 提示输入的是数字
MessageBox.Show("输入为数字"); // 取整数
int temp = (int)num_s; // 判断是否大于0.5
if (num_s - temp > 0.5)
{
// 五入
MessageBox.Show(string.Format("四舍五入后结果为{0}", ((int)num_s + 1).ToString()));
}
else
{
// 四舍
MessageBox.Show(string.Format("四舍五入后结果为{0}", temp.ToString())); }/*end of if (num_s - temp > 0.5) */ }
catch (System.Exception ex)
{
//不是数字
MessageBox.Show(string.Format("输入不是数字{0}", ex.Message)); }/*end of try catch */ }
using System.Collections.Generic;
using System.Text;namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入一个带一位小数的数字");
string s=Console.ReadLine();
try
{
Double num_s=Double.Parse(s);
//是数字
Console.WriteLine("输入为数字");
if(num_s-(int)num_s>=0.5)
Console.WriteLine("四舍五入后结果为{0}",(int)num_s+1);
else
Console.WriteLine("四舍五入后结果为{0}",(int)num_s);
}
catch (System.Exception ex)
{
//不是
Console.WriteLine("输入不是数字{0}", ex);
}
Console.WriteLine("数字");
string ss = Console.ReadLine();
}
}
}楼主,你的代码没问题嘛!!一切正常!
if(num_s-(int)num_s>=0.5)