RadarURL
응용 프로그래밍

자바에서 계산된 double값을 소수점 1자리만 나타내기

by JaeSoo posted Jun 25, 2007
?

Shortcut

PrevPrev Article

NextNext Article

ESCClose

Larger Font Smaller Font Up Down Go comment Print

1. 소숫점 한자리까지 표현하기.


   123.4567 ==> 123.4 또는 123.5(반올림적용)


   System.out.println( ((int)(f*10))/10F ); // ==> 123.4


   System.out.println( Math.round(f*10)/10F ); // ==> 123.5
   System.out.println( new java.text.DecimalFormat("#.#").format(f) ); // ==> 123.5


 


2. 정수부분을 빼고, 소숫점 한자리부분을 표현한다.


  123.456 ==> 0.4 또는 0.5(반올림적용)


  System.out.println( ((int)((f-(int)f)*10))/10F ); // ==> 0.4
  System.out.println( Math.round((f-(int)f)*10)/10F ); // ==> 0.5

출처 : http://kin.naver.com/db/detail.php?d1id=1&dir_id=10106&eid=lshkD8oaLQ2Afzwsj8NuX8P2r3uer0cI&qb=amF2YSC80rz9waEgwNq4rg==

Who's JaeSoo

profile

http://JaeSoo.com Administrator


Articles

1 2 3 4 5 6 7 8 9 10