java.lang.Math.round () 학습
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)
특수성은 다음과 같습니다.
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가 최고지!(디자인상 반올림-결과가 커지는 원칙에 부합한다, 허허)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ORACLE_함수● 내장함수 ● 함수 반환값 존재 ● 반올림 ● 대문자 ● 일부만 가져오기(나이, 시작위치, 개수) ● 함수 만들기 ● 오늘 날짜 만드는 함수: FUNC_TODAY ● 함수 확인 ● 팩토리얼 함수: FUNC_SUM ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.