【问题标题】:Java: Specifying how many numbers after the decimal pointJava:指定小数点后有多少个数字
【发布时间】:2011-12-26 18:50:37
【问题描述】:

我有一个用于计算数据范围的 GUI Java 代码,例如,如果输入了两个值 2.4443.555,则结果将是一个 long double 1.11100000... 等。如何指定小数点后的位数点它应该显示? (例如:%.2f

这是我的代码:

public class Range
{
    public static void main(String args[])
    {
        int num=0; //number of data
        double d; //the data
        double smallest = Integer.MAX_VALUE;
        double largest = Integer.MIN_VALUE;
        double range = 0;

        String Num =
        JOptionPane.showInputDialog("Enter the number of data ");
        num=Integer.parseInt(Num);

        for(int i=0; i<num; i++)    
        {
            String D =
            JOptionPane.showInputDialog("Enter the data ");
            d=Double.parseDouble(D);

            if(d < smallest)
                smallest = d;
            if(d > largest)
                largest = d; 
        }

        range = largest - smallest ; //calculating the range of the input

        JOptionPane.showMessageDialog(null,"Range = "+smallest+"-"+largest+" = "+range,"Range",JOptionPane.PLAIN_MESSAGE);
    }
}

【问题讨论】:

    标签: java double decimal digit


    【解决方案1】:

    您可以使用String.format 定义您喜欢的输出,例如

    String.format("Range = %.4f", range)
    

    显示 4 位小数。

    【讨论】:

    • 这个语句会在gui中显示结果吗?
    • 此方法返回一个字符串表示,因此您可以替换showMessageDialog中的代码。
    【解决方案2】:

    DecimalFormat。下面的示例表单here

    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    public class DecimalFormatExample
    {
        public static void main(String[] args)
        {
            // We have some millons money here that we'll format its look.
            double money = 100550000.75;
    
            // By default to toString() method of the Double data type will print
            // the money value using a scientific number format as it is greater
            // than 10^7 (10,000,000.00). To be able to display the number without
            // scientific number format we can use java.text.DecimalFormat wich
            // is a sub class of java.text.NumberFormat.
    
            // Below we create a formatter with a pattern of #0.00. The # symbol
            // means any number but leading zero will not be displayed. The 0 
            // symbol will display the remaining digit and will display as zero
            // if no digit is available.
            NumberFormat formatter = new DecimalFormat("#0.00");
    
            // Print the number using scientific number format.
            System.out.println(money);
    
            // Print the number using our defined decimal format pattern as above.
            System.out.println(formatter.format(money));
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用这个:

      double x = new Random.nextDouble(); //ex: x = 0.126521478419346598
      x = Math.round(x*1000)/1000.0;  //x will be: 0.126
      

      【讨论】:

        【解决方案4】:

        将此 DecimalFormat 实例添加到方法的顶部:

        DecimalFormat three = new DecimalFormat("0.000");
        
        // or this. this will modify the number to have commas every new thousandths place.
        DecimalFormat three = new DecimalFormat("#,##0.000");
        
        // the three zeros after the decimal point above specify how many decimal places to be accurate to.
        // the zero to the left of the decimal place above makes it so that numbers that start with "0." will display "0." vs just "." If you don't want the "0.", replace that 0 to the left of the decimal point with "#"
        

        然后,调用这个实例并在显示时传递一个字符串:

        String str = 4.651681351;
        display.setText(three.format(str)); // displays 4.652
        

        【讨论】:

          猜你喜欢
          • 2018-08-20
          • 1970-01-01
          • 2021-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多