Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Formatting Numbers and Alignment | Mastering String.format()
Quizzes & Challenges
Quizzes
Challenges
/
Formatting & Parsing in Java

bookFormatting Numbers and Alignment

Mastering number formatting and alignment in Java is essential for producing clear and professional console output. The String.format() method gives you precise control over how numbers and text are displayed. By specifying width, precision, and alignment options, you can ensure that your output is both readable and visually appealing. Understanding these formatting tools is particularly important when you need to present data in columns, such as in tables or reports.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { double price1 = 5.2; double price2 = 15.75; double price3 = 123.456; System.out.println(String.format("%-10s %10s", "Item", "Price")); System.out.println(String.format("%-10s %10.2f", "Apple", price1)); System.out.println(String.format("%-10s %10.2f", "Banana", price2)); System.out.println(String.format("%-10s %10.2f", "Cherry", price3)); } }

In the example above, you see how width and precision specifiers work together to control the appearance of your output. The width specifier (such as 10 in %10.2f) reserves a minimum number of characters for the value, ensuring that columns align neatly. The precision specifier (.2 in %10.2f) controls the number of digits displayed after the decimal point for floating-point numbers. By combining these specifiers, you can format numbers to a desired precision and align them within columns, making your output much easier to read and compare.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { String name1 = "Alice"; String name2 = "Bob"; int score1 = 95; int score2 = 87; System.out.println(String.format("%-10s | %5s", "Name", "Score")); System.out.println(String.format("%-10s | %5d", name1, score1)); System.out.println(String.format("%-10s | %5d", name2, score2)); } }

1. Which flag in String.format() is used for left alignment of text?

2. Fill in the blank: To format a double to 3 decimal places, use the format specifier ______.

3. How does specifying a width in String.format() affect the output?

question mark

Which flag in String.format() is used for left alignment of text?

Select the correct answer

question-icon

Fill in the blank: To format a double to 3 decimal places, use the format specifier ______.

12.346

Click or drag`n`drop items and fill in the blanks

question mark

How does specifying a width in String.format() affect the output?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show me a code example using String.format() with width and precision?

How do I align text to the left or right using String.format()?

What other formatting options are available in Java for console output?

bookFormatting Numbers and Alignment

Scorri per mostrare il menu

Mastering number formatting and alignment in Java is essential for producing clear and professional console output. The String.format() method gives you precise control over how numbers and text are displayed. By specifying width, precision, and alignment options, you can ensure that your output is both readable and visually appealing. Understanding these formatting tools is particularly important when you need to present data in columns, such as in tables or reports.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { double price1 = 5.2; double price2 = 15.75; double price3 = 123.456; System.out.println(String.format("%-10s %10s", "Item", "Price")); System.out.println(String.format("%-10s %10.2f", "Apple", price1)); System.out.println(String.format("%-10s %10.2f", "Banana", price2)); System.out.println(String.format("%-10s %10.2f", "Cherry", price3)); } }

In the example above, you see how width and precision specifiers work together to control the appearance of your output. The width specifier (such as 10 in %10.2f) reserves a minimum number of characters for the value, ensuring that columns align neatly. The precision specifier (.2 in %10.2f) controls the number of digits displayed after the decimal point for floating-point numbers. By combining these specifiers, you can format numbers to a desired precision and align them within columns, making your output much easier to read and compare.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public static void main(String[] args) { String name1 = "Alice"; String name2 = "Bob"; int score1 = 95; int score2 = 87; System.out.println(String.format("%-10s | %5s", "Name", "Score")); System.out.println(String.format("%-10s | %5d", name1, score1)); System.out.println(String.format("%-10s | %5d", name2, score2)); } }

1. Which flag in String.format() is used for left alignment of text?

2. Fill in the blank: To format a double to 3 decimal places, use the format specifier ______.

3. How does specifying a width in String.format() affect the output?

question mark

Which flag in String.format() is used for left alignment of text?

Select the correct answer

question-icon

Fill in the blank: To format a double to 3 decimal places, use the format specifier ______.

12.346

Click or drag`n`drop items and fill in the blanks

question mark

How does specifying a width in String.format() affect the output?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt