• Java——Math的round方法


    代码如下,后面的注释是输出的结果

    public static void main(String[] args) {
            System.out.println(Math.round(0.399));//0
            System.out.println(Math.round(0.4));//0
            System.out.println(Math.round(0.41));//0
            System.out.println(Math.round(0.499));//0
            System.out.println(Math.round(0.5));//1
            System.out.println(Math.round(0.51));//1
            System.out.println(Math.round(0.6));//1
            System.out.println("======================");
            System.out.println(Math.round(-0.6));//-1
            System.out.println(Math.round(-0.51));//-1
            System.out.println(Math.round(-0.5));//0
            System.out.println(Math.round(-0.499));//0
            System.out.println(Math.round(-0.41));//0
            System.out.println(Math.round(-0.4));//0
            System.out.println(Math.round(-0.399));//0
        }

     总结,round的进位是向数轴的右方向进位的,而不是按照数的绝对值进行四舍五入的

    关于Math对三角函数表示的一些知识点查看https://blog.csdn.net/River_Continent/article/details/80637952

    下方把该博文摘录

    一、问题

    三角函数在Java中是怎么表示的?

    二、解答

    2.1、Math中的三角函数

    首先来看一下,Java中的Math怎么表示30°角的弧度制,这里Math中有一个常量PI,就是π;
    我们知道sin30°=0.5;Java中却是近似值:
    这里写图片描述

    2.2、保留小数

    我们采取近似值,保留2位小数,采用四舍五入进位模式,即

    RoundingMode.HALF_UP

    ,达到了一半就进位;
    这里的“一半”是当前进制下,目标所在位权重值的(0.5*进制值)倍,如果是十进制,所在位为个位,那么个位达到了十进制的一半,即5,就向上进1位;
    这里写图片描述

    2.3、例子

    package Math;
    
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    
    public class TestMathTadisans {
        /**
         *@author Taozc
         *@2018-6-10 01:02:16
         */
        public static void main(String[] args) {
            //sin30° =0.5;
            System.out.println("30度角的正弦值:"+new BigDecimal(Math.sin(Math.PI/6)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
            //cos60° =0.5;
            System.out.println("60度角的余弦值:"+new BigDecimal(Math.cos(Math.PI/3)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
            //tan45° =1;
            System.out.println("45度角的正切:"+new BigDecimal(Math.tan(Math.PI/4)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
    
            //正弦值为0.5对应的弧度是π/60.5236,角度是30°;
            System.out.println("正弦值为0.5所对应的反正弦值(对应的弧度):"+new BigDecimal(Math.asin(0.5)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
            //余弦值为0.5对应的弧度是π/31.047,角度是60°;
            System.out.println("余弦值为0.5所对应的反余弦值(对应的弧度):"+new BigDecimal(Math.acos(0.5)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
            //正弦值为0.5的弧度是π/40.5236,角度是45°;
            System.out.println("正切值为1所对应的反正切值(对应的弧度):"+new BigDecimal(Math.atan(1)).setScale(2, RoundingMode.HALF_UP)+"(保留2为小数,四舍五入)");
    
            //正弦值为0.5对应的弧度是π/60.5236,角度是30°;
            System.out.println("正弦值为0.5所对应的反正弦值(对应的角度):"+Math.toDegrees(Math.asin(0.5)));
            //余弦值为0.5对应的弧度是π/31.047,角度是60°;
            System.out.println("余弦值为0.5所对应的反余弦值(对应的角度):"+Math.toDegrees(Math.acos(0.5)));
            //正弦值为0.5的弧度是π/40.5236,角度是45°;
            System.out.println("正切值为1所对应的反正切值(对应的角度):"+Math.toDegrees(Math.atan(1)));
    
            System.out.println("将60度角转化为弧度:"+new BigDecimal(Math.toRadians(60)).setScale(2, RoundingMode.HALF_UP));
            System.out.println("将“六分之一π”弧度转化为角度"+new BigDecimal(Math.toDegrees(Math.PI/6)).setScale(2, RoundingMode.HALF_UP));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    2.4截图:

    这里写图片描述

    三、反思

    这里还有一个不好的地方,就是弧度制转角度的时候,是近似值,但是实际应该是整数;比如30°对应的是六分之一π,但是上面打印出来的值是30.000000000000004,这是为什么呢,这是由于π是无限不循环小数,π除以6,还是一个无限不循环小数,怎么等于30这个值呢?其实,这里应该好好理解下30°,这里别忘了,我们还有一个单位:度,度是什么?度是一个圆,我们切成360份,一份叫一度,30°就是:30×(圆÷360),这里的“度”就是“圆÷360”,我们把汉字“圆”用2π来代替,就变成了“2π÷360”,即“π÷180”,这个就是“°”的本质,她最精确的值(无限不循环);计算机精确度有限,让它表示一个无限不循环小数,她只能近似表达;

    <article class="baidu_pl">                 <div id="article_content" class="article_content clearfix">            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-963e387caa.css">                            <div id="content_views" class="markdown_views">                    <!-- flowchart 箭头图标 勿删 -->                    <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>                                            <h1 id="一问题"><a name="t0"></a><a name="t0"></a>一、问题</h1>
    <p>三角函数在Java中是怎么表示的?</p>
    <h1 id="二解答"><a name="t1"></a><a name="t1"></a>二、解答</h1>
    <h2 id="21math中的三角函数"><a name="t2"></a><a name="t2"></a>2.1、Math中的三角函数</h2>
    <p>首先来看一下,Java中的Math怎么表示30°角的弧度制,这里Math中有一个常量PI,就是π; <br>我们知道sin30°=0.5;Java中却是近似值: <br><img src="https://img-blog.csdn.net/20180610010641172?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1JpdmVyX0NvbnRpbmVudA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>


    <h2 id="22保留小数"><a name="t3"></a><a name="t3"></a>2.2、保留小数</h2>
    <p>我们采取近似值,保留2位小数,采用四舍五入进位模式,即</p>
    <blockquote>  <p>RoundingMode.HALF_UP</p></blockquote>
    <p>,达到了一半就进位; <br>这里的“一半”是当前进制下,目标所在位权重值的(0.5*进制值)倍,如果是十进制,所在位为个位,那么个位达到了十进制的一半,即5,就向上进1位; <br><img src="https://img-blog.csdn.net/20180610010908873?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1JpdmVyX0NvbnRpbmVudA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>


    <h2 id="23例子"><a name="t4"></a><a name="t4"></a>2.3、例子</h2>
    <pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">package Math<span class="hljs-comment">;</span>
    import java<span class="hljs-preprocessor">.math</span><span class="hljs-preprocessor">.BigDecimal</span><span class="hljs-comment">;</span>import java<span class="hljs-preprocessor">.math</span><span class="hljs-preprocessor">.RoundingMode</span><span class="hljs-comment">;</span>
    public class TestMathTadisans {    <span class="hljs-comment">/**     *@author Taozc     *@2018-6-10 01:02:16     */</span>    public static void main(String[] args) {        //sin30° =<span class="hljs-number">0.5</span>;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"30度角的正弦值:"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.sin</span>(Math<span class="hljs-preprocessor">.PI</span>/<span class="hljs-number">6</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>        //cos60° =<span class="hljs-number">0.5</span>;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"60度角的余弦值:"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.cos</span>(Math<span class="hljs-preprocessor">.PI</span>/<span class="hljs-number">3</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>        //tan45° =<span class="hljs-number">1</span>;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"45度角的正切:"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.tan</span>(Math<span class="hljs-preprocessor">.PI</span>/<span class="hljs-number">4</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>
            //正弦值为<span class="hljs-number">0.5</span>对应的弧度是π/<span class="hljs-number">6</span>≈<span class="hljs-number">0.5236</span>,角度是<span class="hljs-number">30</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"正弦值为0.5所对应的反正弦值(对应的弧度):"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.asin</span>(<span class="hljs-number">0.5</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>        //余弦值为<span class="hljs-number">0.5</span>对应的弧度是π/<span class="hljs-number">3</span>≈<span class="hljs-number">1.047</span>,角度是<span class="hljs-number">60</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"余弦值为0.5所对应的反余弦值(对应的弧度):"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.acos</span>(<span class="hljs-number">0.5</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>        //正弦值为<span class="hljs-number">0.5</span>的弧度是π/<span class="hljs-number">4</span>≈<span class="hljs-number">0.5236</span>,角度是<span class="hljs-number">45</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"正切值为1所对应的反正切值(对应的弧度):"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.atan</span>(<span class="hljs-number">1</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP)+<span class="hljs-string">"(保留2为小数,四舍五入)"</span>)<span class="hljs-comment">;</span>
            //正弦值为<span class="hljs-number">0.5</span>对应的弧度是π/<span class="hljs-number">6</span>≈<span class="hljs-number">0.5236</span>,角度是<span class="hljs-number">30</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"正弦值为0.5所对应的反正弦值(对应的角度):"</span>+Math<span class="hljs-preprocessor">.toDegrees</span>(Math<span class="hljs-preprocessor">.asin</span>(<span class="hljs-number">0.5</span>)))<span class="hljs-comment">;</span>        //余弦值为<span class="hljs-number">0.5</span>对应的弧度是π/<span class="hljs-number">3</span>≈<span class="hljs-number">1.047</span>,角度是<span class="hljs-number">60</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"余弦值为0.5所对应的反余弦值(对应的角度):"</span>+Math<span class="hljs-preprocessor">.toDegrees</span>(Math<span class="hljs-preprocessor">.acos</span>(<span class="hljs-number">0.5</span>)))<span class="hljs-comment">;</span>        //正弦值为<span class="hljs-number">0.5</span>的弧度是π/<span class="hljs-number">4</span>≈<span class="hljs-number">0.5236</span>,角度是<span class="hljs-number">45</span>°;        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"正切值为1所对应的反正切值(对应的角度):"</span>+Math<span class="hljs-preprocessor">.toDegrees</span>(Math<span class="hljs-preprocessor">.atan</span>(<span class="hljs-number">1</span>)))<span class="hljs-comment">;</span>
            System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"将60度角转化为弧度:"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.toRadians</span>(<span class="hljs-number">60</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP))<span class="hljs-comment">;</span>        System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"将“六分之一π”弧度转化为角度"</span>+new BigDecimal(Math<span class="hljs-preprocessor">.toDegrees</span>(Math<span class="hljs-preprocessor">.PI</span>/<span class="hljs-number">6</span>))<span class="hljs-preprocessor">.setScale</span>(<span class="hljs-number">2</span>, RoundingMode<span class="hljs-preprocessor">.HALF</span>_UP))<span class="hljs-comment">;</span>    }}<div class="hljs-button {2}" data-title="复制"></div></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><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li></ul></pre>


    <h2 id="24截图"><a name="t5"></a><a name="t5"></a>2.4截图:</h2>
    <p><img src="https://img-blog.csdn.net/20180610011759810?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1JpdmVyX0NvbnRpbmVudA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>
    <h1 id="三反思"><a name="t6"></a><a name="t6"></a>三、反思</h1>
    <p>这里还有一个不好的地方,就是弧度制转角度的时候,是近似值,但是实际应该是整数;比如30°对应的是六分之一π,但是上面打印出来的值是30.000000000000004,这是为什么呢,这是由于π是无限不循环小数,π除以6,还是一个无限不循环小数,怎么等于30这个值呢?其实,这里应该好好理解下30°,这里别忘了,我们还有一个单位:度,度是什么?度是一个圆,我们切成360份,一份叫一度,30°就是:30×(圆÷360),这里的“度”就是“圆÷360”,我们把汉字“圆”用2π来代替,就变成了“2π÷360”,即“π÷180”,这个就是“°”的本质,她最精确的值(无限不循环);计算机精确度有限,让它表示一个无限不循环小数,她只能近似表达;</p>                                    </div><div><div></div></div>                <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-ff98e99283.css" rel="stylesheet">                                </div>            </article>

  • 相关阅读:
    boltdb 学习和实践
    解读-m addrtype --dst-type LOCAL -j DOCKER
    找到linux中当前java的安装位置
    java.library.path是什么?
    Linux 中【./】和【/】和【.】之间有什么区别?
    [SpringMVC]JSON乱码问题
    iOS MVVM的理解与分析
    iOS中为什么必须在主线程操作UI
    几种直播流媒体协议
    IOS硬编码
  • 原文地址:https://www.cnblogs.com/kitor/p/11320302.html
Copyright © 2020-2023  润新知