Skip to content
Sharp

Core Java · interview question

How can you ensure that a double value representing a price is displayed with a minimum of two fractional digits?

Multiple choiceMediumchapter-1-curatedpremiumcore-javaconsole-input-and-output
Choose one answer
Answer and explanation

Correct answer

B. Use System.out.printf("%.2f", value);

Why

The 'printf' method with the format specifier '%.2f' ensures that the double value is formatted to two decimal places, which is traditional for displaying currency values in a readable manner.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →