일반 수치 데이터 처리 유틸리티

황제낙엽 2019.05.12 02:28 조회 수 : 1907

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

import java.math.BigDecimal;

 

public class NumberUtils {

 

/**

* Hex -> 10진수 변환

* @param hex

* @return

*/

public static String hexToDecString(String hex) {

return String.valueOf(hexToDecNumber(hex));

}

 

/**

* Hex -> 10진수 변환

* @param hex

* @return

*/

public static long hexToDecNumber(String hex) {

return Long.parseLong(hex, 16);

}

 

/**

* 10진수 -> Hex 변환

* @param dec

* @return

*/

public static String decToHexString(String dec) {

 

long intDec = Long.parseLong(dec);

return Long.toHexString(intDec).toUpperCase();

}

 

/**

* 10진수 -> Hex 변환

* @param dec

* @return

*/

public static String decToHexString(long dec) {

 

return Long.toHexString(dec).toUpperCase();

}

 

/**

* 수치형 문자열인지 검사

* @param num

* @return

*/

public static boolean isNumber(String num) {

return num == null ? false : num.matches("[0-9.]+");

}

 

    

    /**

     * 실수값에 대해 내림,반올림,올림 처리

     * @param decimal 부동소수

     * @param loc 자릿수 제한 위치 (제한된 자리수의 뒷자리 값으로 올림이나 내림을 취함 -> BigDecimal.setScale의 첫번째 파라미터 newScale를 의미함)

     * @param mode 1 내림 , 2 반올림 , 3 올림

     * @return BigDecimal 처리 결과

     */

    public static BigDecimal decimalRound(double decimal, int loc, int mode) {

 

        BigDecimal bd = new BigDecimal(decimal);

        BigDecimal result = null;

 

        if (mode == 1) {

            result = bd.setScale(loc, BigDecimal.ROUND_DOWN); // 내림

        } else if (mode == 2) {

            result = bd.setScale(loc, BigDecimal.ROUND_HALF_UP); // 반올림

        } else if (mode == 3) {

            result = bd.setScale(loc, BigDecimal.ROUND_UP); // 올림

        }

 

        return result;

    }

    

    /**

     * 실수를 정수로 변환</br>

     * 소수점 부분은 버린다 (반올림이 아님)

     * @param real

     * @return integer

     */

    public static int convertInteger(double real) {

        return new BigDecimal(real).intValue();

    }

}

 
 
번호 제목 글쓴이 날짜 조회 수
» 수치 데이터 처리 유틸리티 file 황제낙엽 2019.05.12 1907
282 한글 초성 중성 종성 분리 유틸리티(자작) file 황제낙엽 2019.05.07 525
281 한글 초성 중성 종성 분리 (자모분리) 황제낙엽 2019.05.07 458
280 한글 인코딩의 이해 1편: 한글 인코딩의 역사와 유니코드 황제낙엽 2019.05.07 501
279 한글 인코딩의 이해 2편: 유니코드와 Java를 이용한 한글 처리 file 황제낙엽 2019.05.07 509
278 응답 헤더의 Content-disposition 속성 황제낙엽 2019.04.16 824
277 StringUtils - 문자열 처리 유틸리티 file 황제낙엽 2019.04.15 517
276 JSON과 GSON 황제낙엽 2019.03.24 594
275 File.length() 에 대하여 황제낙엽 2019.03.24 523
274 File.delete() 와 File.deleteOnExit() 황제낙엽 2019.03.24 2602
273 List to Array / Array to List 황제낙엽 2019.03.24 412
272 Oracle JAVA 유료화에 관련한 최신 기사 황제낙엽 2019.01.23 472
271 [HttpURLConnection, HttpsURLConnection] 코드참조용 샘플프로젝트 secret 황제낙엽 2019.01.18 0
270 Iterator.next() - NoSuchElementException 황제낙엽 2018.10.28 639
269 OracleJDK 유료화 FAQ (Oracle Java 의 유료화에 대한 어느분의 정리) 황제낙엽 2018.10.11 492
268 메일서버(daum.net)에 POP3를 이용하여 메일 가져오기 예제 file 황제낙엽 2018.10.09 1325
267 Sending mail through Java using SMTP of gmail file 황제낙엽 2018.09.13 830
266 Read or get mails using pop in java (using gmail) file 황제낙엽 2018.09.13 11657
265 Collections.sort() , Comparator 황제낙엽 2018.08.23 556
264 JavaMail - Connecting Gmail pop3 server. 황제낙엽 2018.08.20 711