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

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中却是近似值:
Java——Math的round方法

2.2、保留小数

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

RoundingMode.HALF_UP

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

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截图:

Java——Math的round方法

三、反思

 

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

<article class="baidu_pl">                 <div >                                </div>            </article>

相关文章:

  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2021-10-10
  • 2018-06-20
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2021-11-27
  • 2021-06-20
  • 2021-11-20
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案