- 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
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
- Using RoundingMode.UP
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
- Using RoundingMode.DOWN
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
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
- Using RoundingMode.FLOOR
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
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
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
No comments:
Post a Comment