Programming/Kotlin

[Kotlin] Error: Non-terminating decimal expansion; no exact representable decimal result. 였λ₯˜

yuri lee 2023. 1. 2. 22:33
λ°˜μ‘ν˜•

Intro

kotlin 기반의 쑰회 APIλ₯Ό λ§Œλ“€λ˜ 도쀑 μ•„λž˜μ™€ 같은 μ—λŸ¬κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 

 

Non-terminating decimal expansion; no exact representable decimal result.

 

 

Why?

값을 κ΅¬ν•˜κΈ° μœ„ν•΄ divideλ₯Ό 톡해 두 BigDecimal 값을 λ‚˜λˆ μ£Όμ—ˆμŠ΅λ‹ˆλ‹€. λΆ„λͺ… μ—λŸ¬κ°€ μ—†μ—ˆλŠ”λ°, μ™œ μ—λŸ¬κ°€ λ°œμƒν–ˆλŠ”μ§€ ꡬ글링을 ν•˜μ—¬ κ²€μƒ‰ν•΄λ΄€μŠ΅λ‹ˆλ‹€. BigDecimal λ‚˜λˆ„κΈ°λ₯Ό ν•  경우 μ •ν™•ν•œ 값이 떨어지지 μ•ŠμœΌλ©΄ λ‹€μŒμ˜ "Non-terminating decimal expansion; no exact representable decimal result." μ—λŸ¬κ°€ λ°œμƒν•œλ‹€κ³  ν•©λ‹ˆλ‹€.

 

κΈ°μ‘΄ ν…ŒμŠ€νŠΈ ν•˜λ˜ λ°μ΄ν„°μ—μ„œλŠ” μ •ν™•νžˆ 값이 λ–¨μ–΄μ Έ λ‚˜λˆ μ‘ŒκΈ° λ•Œλ¬Έμ— μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šμ•˜μ§€λ§Œ, λ‹€λ₯Έ 데이터 μ…‹μ—μ„œλŠ” 값이 λ‚˜λˆ„μ–΄ 떨어지지 μ•Šμ•„ μ—λŸ¬κ°€ λ°œμƒν•œ κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€. 

How to solve the problem

divide μ‹œ ν•„μˆ˜λ‘œ μžλ¦¬μˆ˜μ™€ 결과셋을 μ§€μ •ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. 

bigDecimal.divide(value, 2, BigDecimal.ROUND_CEILING);
  • 올림 : ROUND_CEILING
  • λ‚΄λ¦Ό : ROUND_FLOOR
  • 반올림 : ROUND_HALF_UP
  • λ°˜λ‚΄λ¦Ό : ROUND_HALF_DOWN

 


https://development-storage.tistory.com/3

https://eclipse4j.tistory.com/189

https://kingjakeu.github.io/java/2020/12/23/bigdecimal/

λ°˜μ‘ν˜•