Format Floating Point Number in Java



What is Floating Point Number?

Floating point number formatting is the process of controlling how floating point numbers (numbers with decimal points) are displayed(printed in a program). For example, if we have a decimal number of 123.45678, the formatted number can be 123.457, 123.46, and 123.5 instead of that number. This makes the numbers easier to read and more consistent.

The Floating point is just a number with decimal points. For example 1.2, 2.45 and 123.4 are the examples of the point numbers.
It helps ensure the number of decimal places, making numerical data easier to read and maintaining a consistent format across different outputs.

Example

Following are the representation of format floating point number ?

float number = 20.932;
float number = 123.45678;

Now, we will give one decimal point to the above float number: 20.932, So, it result the number as 20.9 and next float number = 123.45678, we will give the decimal point as 3. So, results the output as 123.457.

Different Ways to Format Floating Point Number

Here, we are providing three different approaches to get the result of format floating point number ?

Now, we will understand each approach with their examples.

Format Floating Number Using String.format() Method

In Java, to format floating point numbers, we have built-in methods like String.format() that allow us to convert the given floating point number into a desired format.

The String.format() method is used to format the passed data according to the specified format specifiers. Although it is a method of the String class, but it can format various types of data, not just string data.

Example

In this program, we format a given floating point number 123.45678f to 1 decimal place using the String.format() method. It stores the formatted number in a string, and then it prints the output ?

public class Main {
    public static void main(String[] args) {
        float number = 123.45678f;
        String formattedString = String.format("%.1f", number);
        System.out.println("Formatted number = " + formattedString);
    }
}

Output

The above code produces the following result ?

Formatted number= 123.5

Format Floating Number Using DecimalFormat Class

Here, we are discussing another approach (way) to format a given floating-point number using the DecimalFormat class. This class allows us to format numbers according to a specific pattern.

The DecimalFormat is class belongs to the java.text package. It is used to format the decimal numbers by specifying the patterns for the formatting.

Example

In the following program, we format a given floating point number 123.45678f to 2 decimal places using the DecimalFormat class. It creates an instance 'DecimalFormat' object with the pattern #.## and uses this format to format the number ?

import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        float number = 123.45678f;
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        String formattedString = decimalFormat.format(number);

        System.out.println("Formatted number = " + formattedString);
    }
}

Output

Following is the output of the above program ?

Formatted number= 123.46

Format Floating Number Using printf() Method

You can also format the floating-point number using the printf() method.

The printf() method is used to print the formatted output without creating any intermediate strings.

Example

In the example below, we format a given floating point number 123.45678f to 3 decimal places using the printf() method. We specify the format specifier %.3f which ensures that the number will be displayed with "3" digits after the decimal point ?

public class Main{
    public static void main(String[] args) {
        float number = 123.45678f;
        System.out.printf("Formatted number = %.3f\n", number);
    }
}

Output

After executing the above program, the following output will be displayed ?

Formatted number = 123.457
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-13T13:38:06+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements