加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java Numeric Formatting--reference

发布时间:2020-12-14 06:19:21 所属栏目:Java 来源:网络整理
导读: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

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?NumberFormat?that presents numbers in a currency-friendly format.

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?NumberFormat?instance used for currency (actually a?DecimalFormat),automatically applies the appropriate number of digits and appropriate currency symbol based on the locale.

Percentages

The next code listings and associated output demonstrate use of?NumberFormat?to present numbers in percentage-friendly format.

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?NumberFormat?usage demonstrate that by default the instance of?NumberFormat?(actually a?DecimalFormat?in this case) returned by?method has no fractional digits,multiplies the provided number by 100 (assumes that it is the??when provided),and adds a percentage sign (%).

Integers

The small amount of code shown next and its associated output demonstrate use ofNumberFormat?to present numbers in integral format.

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?NumberFormat?method?returns an instance that presents provided numerals as integers.

Fixed Digits

The next code listing and associated output demonstrate using?NumberFormat?to print fixed-point representation of floating-point numbers. In other words,this use ofNumberFormat?allows one to represent a number with an exactly prescribed number of digits to the left of the decimal point ("integer" digits) and to the right of the decimal point ("fraction" digits).

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?NumberFormat?(?and),but have decided this post is already sufficiently lengthy. I may write about these descendants of?NumberFormat?in future blog posts.

reference from:http://java.dzone.com/articles/java-numeric-formatting

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读