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

java double转string

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

<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);"&gt;static</span> <span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> <span class="hljs-title"&gt;test1</span>(<span class="hljs-keyword" style="color:rgb(0,136);"&gt;double</span> dou) {
    String dou_str = Double.toString(dou);
    <span class="hljs-keyword" style="color:rgb(0,136);"&gt;if</span> (dou_str.equals(<span class="hljs-string" style="color:rgb(0,136,0);"&gt;"20160101"</span>)) {
        System.<span class="hljs-keyword" style="color:rgb(0,136);"&gt;out</span>.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"YES!"</span>);
    } <span class="hljs-keyword" style="color:rgb(0,136);"&gt;else</span> {
        System.<span class="hljs-keyword" style="color:rgb(0,0);"&gt;"NO!"</span>);
    }
}

<span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> <span class="hljs-title"&gt;main</span>(String[] args) {
    <span class="hljs-keyword" style="color:rgb(0,136);"&gt;double</span> dou = <span class="hljs-number" style="color:rgb(0,102,102);"&gt;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

      1
      1

    
    

      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!

    (编辑:李大同)

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

      推荐文章
        热点阅读