일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 자청
- JNI
- NDK
- 인스턴스
- 부의추월차선
- SharedPreferences
- Android
- 안드로이드
- 코틀린
- MVVM
- ViewGroup
- java
- mutable
- View
- Kotlin
- 부자아빠가난한아빠
- 가치생산주의
- 타이탄의도구
- 기업가정신
- 비동기
- ViewModel
- 부자의그릇
- 코루틴
- 람다
- AsyncTask
- Coroutines
- 멀티쓰레드
- 동기
- 언스크립티드
- 책리뷰
Archives
- Today
- Total
타이탄의 도구들
[Kotlin] 소수점,원화 표시를 도와주는 함수들 본문
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