타이탄의 도구들

[Kotlin] 소수점,원화 표시를 도와주는 함수들 본문

Dev Tools/Source Code

[Kotlin] 소수점,원화 표시를 도와주는 함수들

Titan04 2022. 7. 29. 18:20
728x90

주식,코인,원화 데이터를 표시할 때 유용한 코틀린 함수들을 만들어보았습니다 :)

fun BigDecimal.stripFivePoints() : BigDecimal {
    /**
     * Decimal 데이터를 5자리까지 버림해서 표현해주는 함수
     * */
    val df = DecimalFormat("0.#####")
    df.roundingMode = RoundingMode.DOWN
    return try {
        df.format(this).toBigDecimal()
    }catch(e: Exception){
        this
    }
}​
fun parsingDecimalFunctionRoundDown(decimalValue: String) : String {
    /**
     * Decimal 데이터를 세자리 수마다 콤마를 찍어주고
     * 소수점이 있다면 5자리까지 버림해서 표현해주는 함수
     * */
    //5자리 이상일 땐 0
    val df = DecimalFormat("0.#####")
        df.roundingMode = RoundingMode.DOWN
    val decimalString = try {
        df.format(decimalValue.toBigDecimal())
     }catch(e: Exception){
        decimalValue
    }
    //정수 세자리 수마다 콤마를 찍어주는 DecimalFormat
    val commaFormat = DecimalFormat("###,###")
    if(decimalString.contains(".")){
        // 소수점이 있으면 정수부분 소수부분을 분리해서
        // 정수 부분은 세자리 수마다 콤마를 찍어주고,
        // (콤마 찍어준 정수).(소수 부분) 을 리턴해준다.
        return try {
            //정수 부분
            val integer = decimalString.substring(0, decimalString.lastIndexOf(".") + 1).toBigDecimal()
            //소수 부분
            val decimal = if(decimalString.substring(decimalString.lastIndexOf(".") + 1).length > 4){
                decimalString.toBigDecimal().toPlainString().substring(decimalString.lastIndexOf(".") + 1)
            }else{
                decimalString.toBigDecimal().toPlainString().substring(decimalString.lastIndexOf(".") + 1)
            }
            "${commaFormat.format(integer)}.$decimal"
        }catch(e: Exception){
            Timber.d("Exception1: $e")
            "0.0"
        }
    }else{
        // 소수점이 없다면 세자리 수마다 콤마만 찍어줌
        return try {
            val decimal = decimalString.toBigDecimal()
            commaFormat.format(decimal)
        }catch(e: Exception){
            Timber.d("Exception2: $e")
            "0.0"
        }
    }
}​
fun parsingDecimalFunction(decimalValue: String) : String {
    /**
     * Decimal 데이터를 세자리 수마다 콤마를 찍어주고
     * 소수점이 있다면 후행 0을 제거하여 표시해주는 함수
     * */
    val decimalString = try {
        //후행 0을 제거
        //stripTrailingZeros()
        decimalValue.toBigDecimal().stripTrailingZeros().toPlainString()
    }catch(e: Exception){
        decimalValue
    }
    //정수 세자리 수마다 콤마를 찍어주는 DecimalFormat
    val commaFormat = DecimalFormat("###,###")
    if(decimalString.contains(".")){
        // 소수점이 있으면 정수부분 소수부분을 분리해서
        // 정수 부분은 세자리 수마다 콤마를 찍어주고,
        // (콤마 찍어준 정수).(소수 부분) 을 리턴해준다.
        return try {
            //정수 부분
            val integer = decimalString.substring(0, decimalString.lastIndexOf(".") + 1).toBigDecimal()
            //소수 부분
            val decimal = decimalString.substring(decimalString.lastIndexOf(".") + 1)
            "${commaFormat.format(integer)}.$decimal"
        }catch(e: Exception){
            "0.0"
        }
    }else{
        // 소수점이 없다면 세자리 수마다 콤마만 찍어줌
        return try {
            val decimal = decimalString.toBigDecimal()
            commaFormat.format(decimal)
        }catch(e: Exception){
            "0.0"
        }
    }
}
fun parsingDecimalFunctionKrw(decimalString: String) : String {
    /**
     * 원화 표시 함수
     * */
    //정수 세자리 수마다 콤마를 찍어주는 DecimalFormat
    val commaFormat = DecimalFormat("###,###")
    if(decimalString.contains(".")){
        // 소수점이 있으면 정수부분 소수부분을 분리해서
        // 정수 부분은 세자리 수마다 콤마를 찍어주고 리턴해준다.
        return try {
            //정수 부분
            val integer = decimalString.substring(0, decimalString.lastIndexOf(".") + 1).toBigDecimal()
            commaFormat.format(integer)
        }catch(e: Exception){
            "0.0"
        }
    }else{
        // 소수점이 없다면 세자리 수마다 콤마만 찍어줌
        return try {
            val decimal = decimalString.toBigDecimal()
            commaFormat.format(decimal)
        }catch(e: Exception){
            "0.0"
        }
    }
}

 

728x90
Comments