Return to site

☕🎓JAVA CERTIFICATION QUESTION: Short Money Format

· java,ocp

Given:

double amount = 42_000.00;

NumberFormat format = NumberFormat.getCompactNumberInstance( Locale.FRANCE, NumberFormat.Style.SHORT );

System.out.println( format.format( amount ) );

What is the output?

* 42 000,00 €

* 42000E

* 42 k

* 42000

#java #certificationquestion #ocp

* 42k

Here's why:

The `NumberFormat.getCompactNumberInstance(Locale.FRANCE, NumberFormat.Style.SHORT)` method in Java is used to format numbers in a compact style for the specified locale.

For `Locale.FRANCE`, the compact number instance will format the number `42000.00` as "42k",

because the `SHORT` style typically represents the thousands with a 'k'.

The currency symbol is not included in the output when using the `getCompactNumberInstance` method,

and the decimal part is not shown for whole numbers when formatted in the `SHORT` style.

Numbers and Currencies: Using Predefined Formats

https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

Class NumberFormat

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/NumberFormat.html

Enum Class NumberFormat.Style

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/NumberFormat.Style.html