java bigdemical比较大小

<div id="article\_content" class="article\_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu\_307" data-dsm="post"> <div class="article-copyright"> <svg class="icon" title="CSDN认证原创" aria-hidden="true" style="width:53px; height: 18px; vertical-align: -4px;"> <use xlink:href="#CSDN\_Cert"></use> </svg>
1 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33451004/article/details/71247041 </div> 2 <div id="content_views" class="markdown_views"> 3 <!-- flowchart 箭头图标 勿删 --> 4 <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg> 5 <p>java中对bigdimical比较大小一般用的是bigdemical的compareTo方法;</p>
<pre class="prettyprint" name="code"><code class="hljs livecodeserver has-numbering">int <span class="hljs-operator">a</span> = bigdemical.compareTo(bigdemical2) <span class="hljs-operator">a</span> = -<span class="hljs-number">1</span>,表示bigdemical小于bigdemical2; <span class="hljs-operator">a</span> = <span class="hljs-number">0</span>,表示bigdemical等于bigdemical2; <span class="hljs-operator">a</span> = <span class="hljs-number">1</span>,表示bigdemical大于bigdemical2; </code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre> <p>2 bigdemical详解</p> <pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering"> BigDecimal bigDecimal = new BigDecimal(<span class="hljs-string">"4.44"</span>)<span class="hljs-comment">;</span> BigDecimal decimal = new BigDecimal(<span class="hljs-number">4.44</span>)<span class="hljs-comment">;</span> BigDecimal valueOf = BigDecimal<span class="hljs-preprocessor">.valueOf</span>(<span class="hljs-number">4.44</span>)<span class="hljs-comment">;</span> BigDecimal bigDecimal2 = new BigDecimal(<span class="hljs-number">999999999999</span>L)<span class="hljs-comment">;</span> BigDecimal valueOf2 = BigDecimal<span class="hljs-preprocessor">.valueOf</span>(<span class="hljs-number">999999999999</span>L)<span class="hljs-comment">;</span> System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"bigDecimal="</span>+bigDecimal)<span class="hljs-comment">;</span> System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"decimal="</span>+decimal)<span class="hljs-comment">;</span> System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"valueOf="</span>+valueOf)<span class="hljs-comment">; </span> System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"bigDecimal2="</span>+bigDecimal2)<span class="hljs-comment">;</span> System<span class="hljs-preprocessor">.err</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"valueOf2="</span>+valueOf2)<span class="hljs-comment">;</span> bigDecimal=<span class="hljs-number">4.44</span> decimal=<span class="hljs-number">4.44000000000000039079850466805510222911834716796875</span> valueOf=<span class="hljs-number">4.44</span> bigDecimal2=<span class="hljs-number">999999999999</span> valueOf2=<span class="hljs-number">999999999999</span> </code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li></ul></pre> <p>可以看出,double类型的数,在new bigdemical()和Bigdemical.valueof()求出的结果是不一样的,因为new bigdemical()内部是利用的double的构造方法,而这个构造方法jdk的描述是,其结果具有一定的不可预料性,很难对小数做出精确的描述;所以,在我们利用bigdemical的时候应尽量将参数变为字符串再进行计算;否则,针对double类型的小数就要用Bigdemical.valueOf(double d)了;</p> <p>3 bigdemical的大于等于;</p> <pre class="prettyprint" name="code"><code class="hljs livecodeserver has-numbering">其实很简单:<span class="hljs-built\_in">new</span> bigdemica(<span class="hljs-operator">a</span>).compareTo(<span class="hljs-built\_in">new</span> bigdemical(b)) &gt;= <span class="hljs-number">0</span> 表示<span class="hljs-operator">a</span>大于等于b;小于等于同理;</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> </div>
1 <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-df60374684.css" rel="stylesheet"> 2 </div>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java图形验证码生成工具类及web页面校验验证码

<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divclass"htmledit\_vi

Mycat入门教程

<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divclass"markdown\_vi

Spring Boot MyBatis 连接数据库

<divid"article\_content"class"article\_contenttrackingad"datamod"popu\_307"datadsm"post"style"overflow:hidden;"<divclass"markdown\_views"<p最近比较忙,没来得及抽时间把MyBati

C++标准库和标准模板库

<divid"article\_content"class"article\_contentclearfixcsdntrackingstatistics"datapid"blog"datamod"popu\_307"datadsm"post"<linkrel"stylesheet"href"https://csd