<p style="color:rgb(85,85,85);font-family:'microsoft yahei';font-size:14px;">
<a href="http://lib.csdn.net/base/java" rel="nofollow" class="replace_word" title="Java 知识库" style="text-decoration:none;color:rgb(223,52,52);font-weight:bold;">Java中,double转string可以用Double.toString(d)的方式。但是,这种方式有隐藏的坑,请大家看仔细了:
<pre class="prettyprint" style="font-family:'Source Code Pro',monospace;font-size:14px;line-height:1.45;color:rgb(51,51,51);border:1px solid rgba(128,128,.0745098);"><code class="hljs cs has-numbering" style="display:block;color:inherit;font-family:'Source Code Pro',monospace;">package hello;
<span class="hljs-keyword" style="color:rgb(0,136);">public <span class="hljs-keyword" style="color:rgb(0,136);">class DoubleToString {
<span class="hljs-keyword" style="color:rgb(0,136);">static</span> <span class="hljs-keyword" style="color:rgb(0,136);">void</span> <span class="hljs-title">test1</span>(<span class="hljs-keyword" style="color:rgb(0,136);">double</span> dou) {
String dou_str = Double.toString(dou);
<span class="hljs-keyword" style="color:rgb(0,136);">if</span> (dou_str.equals(<span class="hljs-string" style="color:rgb(0,136,0);">"20160101"</span>)) {
System.<span class="hljs-keyword" style="color:rgb(0,136);">out</span>.println(<span class="hljs-string" style="color:rgb(0,0);">"YES!"</span>);
} <span class="hljs-keyword" style="color:rgb(0,136);">else</span> {
System.<span class="hljs-keyword" style="color:rgb(0,0);">"NO!"</span>);
}
}
<span class="hljs-keyword" style="color:rgb(0,136);">void</span> <span class="hljs-title">main</span>(String[] args) {
<span class="hljs-keyword" style="color:rgb(0,136);">double</span> dou = <span class="hljs-number" style="color:rgb(0,102,102);">20160101</span>;
test1(dou);
}
}
<ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221,221);list-style:none;text-align:right;">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
!
import java.text.NumberFormat;
<span class="hljs-keyword" style="color:rgb(0,136);">void <span class="hljs-title">test2(<span class="hljs-keyword" style="color:rgb(0,136);">double dou) {
Double dou_obj = <span class="hljs-keyword" style="color:rgb(0,136);">new Double(dou);
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(<span class="hljs-keyword" style="color:rgb(0,136);">false);
String dou_str = nf.format(dou_obj);
System.<span class="hljs-keyword" style="color:rgb(0,0);">"dou_str is:" + dou_str);
<span class="hljs-keyword" style="color:rgb(0,102);">20160101;
test2(dou);
}
}
<ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221);list-style:none;text-align:right;">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
:
YES! (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|