- 0:Defines an digit.Zero inculded
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00.000");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result will be: FORMAT:95.570
- #:Defines a digit but zero denotes the absence of this digit
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00.00#");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result will be: FORMAT:95.57
- E:Used in scientific notation to seperate mantessa and exponent
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00.00#E0");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
Which gives as a result:FORMAT:95.57E0
- %:Denotes percentage.It multplies the number with 100
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00.00%");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result is:FORMAT:9557.01%
- , : which is used as a grouping seperator
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("0,00.00");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result is:FORMAT:0,95.57
\u2030:
Denotes per mile.It multiplies the number with 1000
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("000.00\u2030");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result will be:FORMAT:95570.10‰
\u00A4
:Denotes a currency symbol.Default value is $
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("#00.00\u00A4");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result is:FORMAT:95.57$
- ' : Is used for quoting special charachters.
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("'#'00.000");
System.out.println("FORMAT:" + decimalFormat.format(95.5701));
}
}
And the result will be:FORMAT: #95.570
As for the rounding part,the default rounding stragedy of DecimalFormat is
RoundingMode.HALF_EVEN
which means that the number will be rounded to the nearest even integer.Of course DecimalFormat's rounding can change using setRoundingMode and the appropriate RoundingMode constants which we will see with an example.
- Using RoundingMode.HALF_EVEN
As we already seen RoundingMode.HALF_EVEN is the DecimalFormat's default rounding mode.Note that with RoundingMode.HALF_EVEN a number will be rounded to the nearest even integer.For example :
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
System.out.println("FORMAT:" + decimalFormat.format(95.5));
}
}
The result will be:FORMAT:96 but in this case:
package decimalformatexample;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
System.out.println("FORMAT:" + decimalFormat.format(94.5));
}
}
the result will be:FORMAT:94
When using RoundingMode.UP the rounded number will always increment.For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.UP);
System.out.println("FORMAT:" + decimalFormat.format(94.1));
}
}
The result will be: FORMAT:95
RoundingMode.DOWN is acctually the opposite of RoundingMode.UP.In other words,while in RoundingMode.UP the rounding number will always be incremented to its nearest integer,in RoundingMode.DOWN the rounded number will decrement to its nearest integer.For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.DOWN);
System.out.println("FORMAT:" + decimalFormat.format(94.5));
}
}
The result will be:FORMAT:94
- Using RoundingMode.CEILING
RoundingMode.CEILING acts as RoundingMode.UP for possitive numbers and as RoundingMode.DOWN for negative.For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.CEILING);
System.out.println("FORMAT:" + decimalFormat.format(94.5));
}
}
The result will be: FORMAT:95.While in this case:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.CEILING);
System.out.println("FORMAT:" + decimalFormat.format(-94.5));
}
}
The result will be:FORMAT: -94
RoundingMode.Floor acts as RoundingMode.DOWN for possitive numbers and as RoundingMode.UP for negative numbers.For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
System.out.println("FORMAT:" + decimalFormat.format(94.5));
}
}
The result will be:FORMAT:94 while in this case:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
System.out.println("FORMAT:" + decimalFormat.format(-94.5));
}
}
The result will be: FORMAT: - 95
- Using RoundingMode.HALF_DOWN
In RoundingMode.HALF_DOWN a number is rounded towards its "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for RoundingMode.UP if the discarded fraction is > 0.5; otherwise, behaves as for RoundingMode.DOWN.For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
System.out.println("FORMAT:" + decimalFormat.format(91.6));
}
}
The result will be:FORMAT:92 While in this case:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
System.out.println("FORMAT:" + decimalFormat.format(91.5));
}
}
The result will be:FORMAT:91
- Using RoundingMode.HALF_UP
In RoundingMode.HALF_UP the number is rounded towards "nearest neighbor" unless both neighbors are equidistant, in which case is rounded up. Behaves as for RoundingMode.UP if the discarded fraction is >= 0.5; otherwise, behaves as for RoundingMode.DOWN. For example:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
System.out.println("FORMAT:" + decimalFormat.format(91.5));
}
}
The result will be:FORMAT:92 while in this case:
package decimalformatexample;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("00");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
System.out.println("FORMAT:" + decimalFormat.format(91.4));
}
}
Sumber