DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 6 of 59
←PreviousPrevNextNext→

Java Print Numbers - Complete Guide

Printing Numbers in Java

Printing numbers requires understanding how to format numerical data for clear and readable output.

You can display all numeric types such as int, double, float, and long. Java also supports calculations directly inside output statements.

Basic Number Printing

The simplest way to print numbers is by concatenating them with strings or directly passing them to output methods.

Example
public class NumberPrinting {
    public static void main(String[] args) {
        int age = 25;
        double price = 19.99;
        float temperature = 98.6f;
        long population = 7800000000L;

        // Basic number printing
        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
        System.out.println("Temperature: " + temperature);
        System.out.println("World Population: " + population);

        // Inline calculations
        int x = 10, y = 20;
        System.out.println("Sum: " + (x + y));
        System.out.println("Product: " + (x * y));
        System.out.println("Average: " + ((x + y) / 2.0));
    }
}
Output
Age: 25
Price: 19.99
Temperature: 98.6
World Population: 7800000000
Sum: 30
Product: 200
Average: 15.0

Number Formatting with printf

For precise control, printf supports format specifiers for decimals, integers, padding, and more.

Example
public class NumberFormatting {
    public static void main(String[] args) {
        double value = 12345.6789;

        // Floating-point formats
        System.out.printf("Default: %f%n", value);
        System.out.printf("Two decimals: %.2f%n", value);
        System.out.printf("No decimals: %.0f%n", value);
        System.out.printf("With commas: %,f%n", value);
        System.out.printf("Scientific: %e%n", value);

        // Integer formats
        int number = 42;
        System.out.printf("Decimal: %d%n", number);
        System.out.printf("Octal: %o%n", number);
        System.out.printf("Hexadecimal: %x%n", number);
        System.out.printf("Padded: %05d%n", number);

        // Currency-like formatting
        double amount = 1234.567;
        System.out.printf("Currency: $%,.2f%n", amount);
    }
}
Output
Default: 12345.678900
Two decimals: 12345.68
No decimals: 12346
With commas: 12,345.678900
Scientific: 1.234568e+04
Decimal: 42
Octal: 52
Hexadecimal: 2a
Padded: 00042
Currency: $1,234.57

Advanced Number Display

For more advanced formatting, Java provides NumberFormat and DecimalFormat for locale-specific and custom patterns.

Example
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Locale;

public class AdvancedFormatting {
    public static void main(String[] args) {
        double number = 1234567.8912;

        // Locale-based formatting
        NumberFormat usFormat = NumberFormat.getInstance(Locale.US);
        NumberFormat frFormat = NumberFormat.getInstance(Locale.FRANCE);

        System.out.println("US Format: " + usFormat.format(number));
        System.out.println("French Format: " + frFormat.format(number));

        // Currency formatting
        NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println("US Currency: " + currency.format(number));

        // Custom patterns
        DecimalFormat customFormat = new DecimalFormat("###,###.###");
        System.out.println("Custom Format: " + customFormat.format(number));

        DecimalFormat percentFormat = new DecimalFormat("##.##%");
        System.out.println("Percentage: " + percentFormat.format(0.856));
    }
}
Output
US Format: 1,234,567.891
French Format: 1 234 567,891
US Currency: $1,234,567.89
Custom Format: 1,234,567.891
Percentage: 85.6%
Test your knowledge: Java Print Numbers - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 6 of 59
←PreviousPrevNextNext→