Java Numeric Formatting--reference
I can think of numerous times when I have seen others write unnecessary Java code and I have written unnecessary Java code because of lack of awareness of a??class that already provides the desired functionality. One example of this is the writing of time-related constants using hard-coded values such as?,?,?,and?when??provides a better,standardized approach. In this post,I look at another example of a class that provides the functionality I have seen developers often implement on their one:?. The??class is part of the?,which also includes the frequently used??and??classes.??is an abstract class (no public constructor) and instances of its descendants are obtained via overloaded static methods with names such as?,?,and?. Currency The next code listing demonstrates calling?to get an instance of? Demonstrating NumberFormat's Currency Support ?
?
?
?
" ?
" ?
" ?
?
?
?
?
?
?
?
?
?
When the above code is executed,the results are as shown next: ================================================================================== = Currency NumberFormat Examples ================================================================================== 15.5 -> $15.50 15.54 -> $15.54 15.345 -> $15.35 Concurrency: USD ISO 4217 Currency Code: USD ISO 4217 Numeric Code: 840 Currency Display Name: US Dollar Currency Symbol: $ Currency Default Fraction Digits: 2 The above code and associated output demonstrate that the? Percentages The next code listings and associated output demonstrate use of? Demonstrating NumberFormat's Percent Format ?
?
?
?
?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
" ?
================================================================================== = Percentage NumberFormat Examples ================================================================================== 1 -> 100% 75/100 -> 0% .75 -> 75% 75.0/100 -> 75% 83/93 -> 0% 93/83 -> 100% .5 -> 50% .912 -> 91% ---- Setting Minimum Fraction Digits to 1: 1 -> 100.0% .75 -> 75.0% 75.0/100 -> 75.0% .912 -> 91.2% The code and output of the percent? Integers The small amount of code shown next and its associated output demonstrate use of Demonstrating NumberFormat's Integer Format ?
?
?
?
" ?
" ?
" ?
" ?
================================================================================== = Integer NumberFormat Examples ================================================================================== 7.65 -> 8 7.5 -> 8 7.49 -> 7 -23.23 -> -23 As demonstrated in the above code and associated output,the? Fixed Digits The next code listing and associated output demonstrate using? Demonstrating NumberFormat for Fixed-Point Numbers ?
?
?
?
================================================================================== = NumberFormat Fixed-Point Examples ================================================================================== 234.23 1 0.23 0.34 0.35 1 The above code and associated output demonstrate the fine-grain control of the minimum number of "integer" digits to represent to the left of the decimal place (at least one,so zero shows up when applicable) and the maximum number of "fraction" digits to the right of the decimal point. Although not shown,the maximum number of integer digits and minimum number of fraction digits can also be specified. Conclusion I have used this post to look at how??can be used to present numbers in different ways (currency,percentage,integer,fixed number of decimal points,etc.) and often means no or reduced code need be written to massage numbers into these formats. When I first began writing this post,I envisioned including examples and discussion on the direct descendants of? reference from:http://java.dzone.com/articles/java-numeric-formatting (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |