java.lang.Math.round () 학습

2887 단어 반올림정확도

public class MathTest {public static void main (String [] args) {System.out.println ("소수점 뒤 첫 번째 자리=5), System.out.println ("정수:Math.round(11.5)="+ Math.round(11.5)), System.out.println ("음수:Math.round(-11.5)="+ h.Matround(-11.5), System.println."소수점 다음 첫 번째 자리<5");           System.out.println(정수: Math.round(11.46) = + Math.round(11.46));           System.out.println("음수: Math.round(-11.46)="+ Math.round(-11.46));           System.out.println();             System.out.println("소수점 후 첫 번째>5");           System.out.println(정수: Math.round(11.68) = + Math.round(11.68));           System.out.println("음수: Math.round(-11.68)="+ Math.round(-11.68));}} 실행 결과: 1, 소수점 뒤의 첫 번째 자리 = 52, 정수: Math.round(11.5) = 123, 음수: Math.round(-11.5)=-114, 5, 소수점 뒤 1위<56, 정수:Math.round(11.46) = 117, 음수: Math.round(-11.46)=-118, 9, 소수점 뒤 1위>510, 정수:Math.round(11.68) = 1211, 음수: Math.round(-11.68)=-12는 상기 예의 운행 결과에 따라 우리는 다음과 같은 방식으로 정리할 수 있다. 1. 매개 변수의 소수점 후 첫 번째<5, 연산 결과는 매개 변수의 정수 부분이다.2. 매개 변수의 소수점 후 첫 번째 비트>5, 연산 결과는 매개 변수의 정수 부분의 절대값 +1, 기호(즉 양과 음)는 변하지 않는다.3. 매개 변수의 소수점 뒤의 첫 번째 자리 = 5, 정수 연산 결과는 정수 부분 +1, 음수 연산 결과는 정수 부분이다.
 
종결: 5보다 크면 모두 더하기, 5정수 더하기, 5보다 작으면 모두 더하지 않는다.
전재:http://blog.sina.com.cn/s/blog_4b06743c0100lst3.html
-----------------------------------------------------------------------------
이상은 원래의 분석입니다~ 아무래도 문제가 있는 것 같습니다. 예를 들어Math.round(-11.5345) == -12; 
다음은 JDK API 1.6을 참조하십시오.
round
public static long round(double a)

가장 가까운 매개변수의long .결과는 정수로 반올림: 1/2을 더하여 결과를 floor로 호출하고 얻은 결과를 강제로 변환합니다long 유형.즉, 결과는 다음 표현식의 값과 같습니다.
 
(long)Math.floor(a + 0.5d)

특수성은 다음과 같습니다.
  • 매개변수가 NaN이면 결과는 0입니다.
  • 만약 결과가 마이너스 무한대 또는 Long.MIN_VALUE보다 작은 값이라면 결과는 Long.MIN_VALUE의 값과 같다.
  • 만약 매개 변수가 정무한대 또는 Long.MAX_VALUE보다 큰 값이라면 결과는 Long.MAX_VALUE의 값과 같다.

  •  
    매개변수:a - 반올림은long의 부동 소수점 값.
    반환:
    반올림은 가장 가까운 것이다long 값의 매개 변수 값.
    분명히 이것이야말로 라운드의 진정한 응용 방법이다.
    (long)Math.floor(a + 0.5d)

    이렇게 하면 그렇게 많은 추측과 억단이 없고 기억하는 데 신경을 써야 한다.
    Math.round(11.5)=12         (long)Math.floor(11.5+0.5)=12Math.round(-11.5)=-11  (long)Math.floor(-11.5+0.5)=-11
    Math.round(11.5345)=12  (long)Math.floor(11.5345+0.5)=12    Math.round(-11.5345)=-12 (long)Math.floor(-11.5345+0.5)=-12 
    Math.round(11.46)=11  (long)Math.floor(11.46+0.5)=11Math.round(-11.46)=-11  (long)Math.floor(-11.46+0.5)=-11
    Math.round(11.68)=12  (long)Math.floor(11.68+0.5)=12 Math.round(-11.68)=-12  (long)Math.floor(-11.68+0.5)=-12
    .............
    헤헤~ API가 최고지!(디자인상 반올림-결과가 커지는 원칙에 부합한다, 허허)

    좋은 웹페이지 즐겨찾기