Java: Specifying how many numbers after the decimal point

12,405

Solution 1

You can use String.format to define the output you like, e.g.

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

to show 4 decimal places.

Solution 2

DecimalFormat. Below example form 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));
    }
}

Solution 3

Use this:

double x = new Random.nextDouble(); //ex: x = 0.126521478419346598
x = Math.round(x*1000)/1000.0;  //x will be: 0.126
Share:
12,405
Aisha S
Author by

Aisha S

Updated on June 27, 2022

Comments

  • Aisha S
    Aisha S about 2 years

    I have a GUI Java code for calculating the range of data for example if two values entered 2.444 and 3.555 the result will be a long double 1.11100000... etc. How do I specify how many digits after the decimal point it should display? (ex: %.2f)

    This is my code:

    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);
        }
    }
    
  • Aisha S
    Aisha S over 12 years
    is this statement will display the result in gui?
  • Howard
    Howard over 12 years
    This method returns a string representation so you can replace the code inside showMessageDialog.