日期:2014-05-20 浏览次数:20845 次
/**
* 判断一个double能否转换为一个int
* @param num double
* @return boolean
*/
public static boolean doubleIsInt(double num)
{
Boolean flag = false;// 没碰到小数点时候标记是false
int n = 0;// 计数器
char[] charArray = String.valueOf(num).toCharArray();
for (char c : charArray)
{
if (c == '.')
{
flag = true;
continue;
}
if (flag && Integer.valueOf(String.valueOf(c)) > 0)
{
n++;
}
}
if (n > 0)
{
return false;
}
return true;
}
public static void main(String[] args)
{
//double转换为String
double d1 = 1.0000001;
double d2 = 1.0000000;
String strD1 = String.valueOf(d1);
String strD2 = String.valueOf(d2);
//正则式
String regex = "^(\\d+)\\.?[0]*$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(strD1);
if(m.matches())
{