have a tryString str = ""; double value = 0d; while (true) { value = JOptionPane.showInputDialog("input your value"); try { value = Double.parseDouble(str); value = round(value*100)/100d; break; } catch (Exceptio e) { //do nothing } }
上面写错了 String str = ""; double value = 0d; while (true) { str = JOptionPane.showInputDialog("input your value"); //上面写错了,获得输入 try { value = Double.parseDouble(str); //把输入字符串变为double类型 value = Math.round(value*100)/100d; //四舍五入可以用Math的round方法,具体保留几位自己换算 break; } catch (Exceptio e) { //do nothing } }
1。 Format f = new DecimalFormat(".00"); System.out.println(Double.parseDouble(f.format(1111.7111818))); //输出1111.722。 Pattern p = Pattern.compile("^\\d+\\.\\d+$"); Matcher m = p.matcher("0001.2a34"); if(m.find()){ System.out.println(m.group()); } else{ JOptionPane.showMessageDialog(null,"not a decimal"); } 也可以用Double.parseDouble(String),有异常抛出则不是小数,当然要先indexOf(".")是否为-1
String st = 屏幕输入; double d = Double.parseDouble(st); if (d < 0) { System.out.println("请输入正数!"); } else if (st.indexOf(".") == -1) { System.out.println("请输入小数!"); } else if (st.indexOf(".") > ( -1)) { System.out.println(Math.round(d * 100) / 100d); }
提交者:webmaster 作者:zy 发布时间:2005年7月31日 浏览次数:310 --------------------------------------------------------------------------------
/*
* Create Date: 2005-7-27
* Company: Java天下(北京)
* Author: zy
*/
package com.wnetw.util;
/**
* <p>Java天下核心技术研发部</p>
* @author zy
*
* 数学运算
*/
public class Math {
/**
* 四舍五入,对指定数字进行保留小数位转换
* @param inNumber - double 将转换的数字
* @param param - int 保留的小数位
* */
public static String round(double inNumber,int param){
String format="#.";
for(int i=0;i<param;i++){
format=format.concat("#");
}
//去掉多余小数点
if(param==0){
format=format.substring(0,format.toString().length()-1);
}
java.text.DecimalFormat df =new java.text.DecimalFormat(format);
return df.format(inNumber);
}
public static void main(String []args){
System.out.println(Math.round(69.8887,2));
}
}
double value = 0d;
while (true) {
value = JOptionPane.showInputDialog("input your value");
try {
value = Double.parseDouble(str);
value = round(value*100)/100d;
break;
} catch (Exceptio e) {
//do nothing
}
}
String str = "";
double value = 0d;
while (true) {
str = JOptionPane.showInputDialog("input your value"); //上面写错了,获得输入
try {
value = Double.parseDouble(str); //把输入字符串变为double类型
value = Math.round(value*100)/100d; //四舍五入可以用Math的round方法,具体保留几位自己换算
break;
} catch (Exceptio e) {
//do nothing
}
}
负数if (value<0),正数if (str.indexOf(".")<0)//判断有无小数点
能说的就这么多了,你自己看着改吧
Format f = new DecimalFormat(".00");
System.out.println(Double.parseDouble(f.format(1111.7111818)));
//输出1111.722。 Pattern p = Pattern.compile("^\\d+\\.\\d+$");
Matcher m = p.matcher("0001.2a34");
if(m.find()){
System.out.println(m.group());
}
else{
JOptionPane.showMessageDialog(null,"not a decimal");
}
也可以用Double.parseDouble(String),有异常抛出则不是小数,当然要先indexOf(".")是否为-1
double d = Double.parseDouble(st);
if (d < 0) {
System.out.println("请输入正数!"); }
else if (st.indexOf(".") == -1) {
System.out.println("请输入小数!");
}
else if (st.indexOf(".") > ( -1)) {
System.out.println(Math.round(d * 100) / 100d);
}