• toPlainString() 和 toString()(转载)


             <!--一个博主专栏付费入口结束-->
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-d284373521.css">
                                        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-d284373521.css">
                <div class="htmledit_views" id="content_views">
                                            <p><br></p><p></p><h2 style="padding:0px;font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;background-color:rgb(255,255,255);">函数 toPlainString() 和 toString()</h2><p style="font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;background-color:rgb(255,255,255);">对于 BigDecimal b ; (b=(0.4321)^ 20)&nbsp;<br>String s = b.toPlainString() ;&nbsp;<br>System.out.println(s) ;&nbsp;<br>输出为:&nbsp;<br>0.00000005148554641076956121994511276767154838481760200726351203835429763013462401</p><p style="font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;background-color:rgb(255,255,255);">若String s = b.toString() ;&nbsp;<br>输出为:&nbsp;<br>5.148554641076956121994511276767154838481760200726351203835429763013462401E-8</p><p style="font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;background-color:rgb(255,255,255);">在得到的小数,要对其小数点后面的零进行处理,用到:BigDecimal stripTrailingZeros() 返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal。</p><p style="font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;background-color:rgb(255,255,255);">对得到的数据用科学计数法就用:String toPlainString() 返回不带指数字段的此 BigDecimal 的字符串表示形式。<span style="font-weight:700;">通俗来讲就是直接显示,不用科学计数法表示。</span></p><br><pre><code class="language-java hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> java.math.BigDecimal;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BigDecimalDemo</span> </span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        BigDecimal bg = <span class="hljs-keyword">new</span> BigDecimal(<span class="hljs-string">"1E11"</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        System.out.println(bg.toEngineeringString());</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        System.out.println(bg.toPlainString());</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        System.out.println(bg.toString());</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button signin" data-title="登录后复制" onclick="hljs.signin(event)"></div></pre><br>输出<br> 100E+9<br> 100000000000<br> 1E+11<br>                                    </div>
                    </div>
    不一样的烟火
  • 相关阅读:
    GCDPlot已提交到Sourceforge,成为开源项目[GCDPlot become a open source software in sourceforge]
    GCDPlot 0.32
    谈谈“科技人员和白领是购买盗版的主力”
    c正则匹配小计
    C++ 模板惯用法
    C++模板常用使用方法介绍
    常用正则表达式
    C++ 容易忘的基本语法和特性
    C++ 对象资源管理惯用法
    Int? 代表什么意思, 可为空的值类型(Nullable<T>)需要注意的地方
  • 原文地址:https://www.cnblogs.com/cstdio1/p/11874413.html
Copyright © 2020-2023  润新知