سؤال

I want to get the exponent of a BigDecimal.

`1M` (`1E0M`)      -> `0`
`10M` (`1E1M`)     -> `1`
`11M` (`1.1E1M`)   -> `1`
`1E2M`             -> `2`
`1.0E2M`           -> `2`
`100M` (`1.00E2M`) -> `2`

scale, at least by itself, is not what I need. I would really rather not have to use .toPlainString and hack something around that.

I'm a little surprised that the exponent I want isn't part of the internal representation of a BigDecimal.

I'm using BigDecimal from Clojure, but any logic that works for Java is welcome too.

هل كانت مفيدة؟

المحلول

(defn exp<-bigdec
  "Returns the exponent, b, from a BigDecimal in the form a * 10 ^ b."
  [x]
  (- (.precision x) (.scale x) 1))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top