Java DecimalFormat: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:


* http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
* http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html


=Internal=
=Internal=
Line 10: Line 9:
* [[java String.format()|String.format()]]
* [[java String.format()|String.format()]]


 
=Overview=
!!!Overview


<font color=red>'#' means display the digit if ? It does not fill up with space.</font>
<font color=red>'#' means display the digit if ? It does not fill up with space.</font>


!!!Simplest
=Simplest=


{{{
<pre>
    
    
    private static final Format THREAD_NUMBER_FORMAT = new DecimalFormat("000");
private static final Format THREAD_NUMBER_FORMAT = new DecimalFormat("000");
 
...
 
THREAD_NUMBER_FORMAT.format(index);
 
</pre>
 
=Float/Double with Two Decimals=
 
<syntaxhighlight lang='java'>
 
private static final Format AVERAGE_DURATION_FORMAT = new DecimalFormat("#.00");
 
...


    ...
AVERAGE_DURATION_FORMAT.format(value);


    THREAD_NUMBER_FORMAT.format(index);
</syntaxhighlight>


}}}
=Percentage=


!!!Float/Double with Two Decimals
Multiply with 100 and display as percentage, with the amount of decimals specified:


{{{
<syntaxhighlight lang='java'>
    private static final Format AVERAGE_DURATION_FORMAT = new DecimalFormat("#.00");


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


    AVERAGE_DURATION_FORMAT.format(value);
...


}}}
PERCENTAGE_FORMAT.format(value);


__Referenced by:__\\
</syntaxhighlight>
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Latest revision as of 23:51, 6 February 2018

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);