1.我想用JOptionPane.showInputDialog输入一个小数,如果输入的不是小数,怎么弹出showMessageDialog给予提示要输入小数.
2.我输入小数后,怎么进行四舍五入,小数点后只保留2位,小数点之前的整数不变

解决方案 »

  1.   

    Java实现四舍五入  
    提交者: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));
        }
    }
     
      

  2.   

    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
        }
    }
      

  3.   

    上面写错了
    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
        }
    }
      

  4.   

    输错时想弹出对话框,在上面的//do nothing的地方加上JOptionPane.showMessageDialog(this, "请输入小数");就可以了
      

  5.   

    我能告诉你的只是思路,具体你要根据你的自己要求修改
    负数if (value<0),正数if (str.indexOf(".")<0)//判断有无小数点
    能说的就这么多了,你自己看着改吧
      

  6.   

    JOptionPane.showMessageDialog(this, "请输入小数");
      

  7.   

    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
      

  8.   

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