Java DecimalFormat

From NovaOrdis Knowledge Base
Revision as of 23:51, 6 February 2018 by Ovidiu (talk | contribs) (→‎Percentage)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

External

Internal

Overview

'#' means display the digit if ? It does not fill up with space.

Simplest

   
private static final Format THREAD_NUMBER_FORMAT = new DecimalFormat("000");

...

THREAD_NUMBER_FORMAT.format(index);

Float/Double with Two Decimals

private static final Format AVERAGE_DURATION_FORMAT = new DecimalFormat("#.00");

...

AVERAGE_DURATION_FORMAT.format(value);

Percentage

Multiply with 100 and display as percentage, with the amount of decimals specified:

private static final Format PERCENTAGE_FORMAT = new DecimalFormat("%0.00");

...

PERCENTAGE_FORMAT.format(value);