Set specific precision of a BigDecimal

前端 未结 4 2129
[愿得一人]
[愿得一人] 2020-11-28 13:28

I have an XSD that requires me to use a BigDecimal for a lat/lon. I currently have the lat/lon as doubles, and convert them to BigDecimal, but I am only required to use abo

相关标签:
4条回答
  • 2020-11-28 13:39

    Try this code ...

        Integer perc = 5;
        BigDecimal spread = BigDecimal.ZERO; 
        BigDecimal perc = spread.setScale(perc,BigDecimal.ROUND_HALF_UP);
        System.out.println(perc);
    

    Result: 0.00000

    0 讨论(0)
  • 2020-11-28 13:49
     BigDecimal decPrec = (BigDecimal)yo.get("Avg");
     decPrec = decPrec.setScale(5, RoundingMode.CEILING);
     String value= String.valueOf(decPrec);
    

    This way you can set specific precision of a BigDecimal.

    The value of decPrec was 1.5726903423607562595809913132345426 which is rounded off to 1.57267.

    0 讨论(0)
  • 2020-11-28 13:53

    The title of the question asks about precision. BigDecimal distinguishes between scale and precision. Scale is the number of decimal places. You can think of precision as the number of significant figures, also known as significant digits.

    Some examples in Clojure.

    (.scale     0.00123M) ; 5
    (.precision 0.00123M) ; 3
    

    (In Clojure, The M designates a BigDecimal literal. You can translate the Clojure to Java if you like, but I find it to be more compact than Java!)

    You can easily increase the scale:

    (.setScale 0.00123M 7) ; 0.0012300M
    

    But you can't decrease the scale in the exact same way:

    (.setScale 0.00123M 3) ; ArithmeticException Rounding necessary
    

    You'll need to pass a rounding mode too:

    (.setScale 0.00123M 3 BigDecimal/ROUND_HALF_EVEN) ;
    ; Note: BigDecimal would prefer that you use the MathContext rounding
    ; constants, but I don't have them at my fingertips right now.
    

    So, it is easy to change the scale. But what about precision? This is not as easy as you might hope!

    It is easy to decrease the precision:

    (.round 3.14159M (java.math.MathContext. 3)) ; 3.14M
    

    But it is not obvious how to increase the precision:

    (.round 3.14159M (java.math.MathContext. 7)) ; 3.14159M (unexpected)
    

    For the skeptical, this is not just a matter of trailing zeros not being displayed:

    (.precision (.round 3.14159M (java.math.MathContext. 7))) ; 6 
    ; (same as above, still unexpected)
    

    FWIW, Clojure is careful with trailing zeros and will show them:

    4.0000M ; 4.0000M
    (.precision 4.0000M) ; 5
    

    Back on track... You can try using a BigDecimal constructor, but it does not set the precision any higher than the number of digits you specify:

    (BigDecimal. "3" (java.math.MathContext. 5)) ; 3M
    (BigDecimal. "3.1" (java.math.MathContext. 5)) ; 3.1M
    

    So, there is no quick way to change the precision. I've spent time fighting this while writing up this question and with a project I'm working on. I consider this, at best, A CRAZYTOWN API, and at worst a bug. People. Seriously?

    So, best I can tell, if you want to change precision, you'll need to do these steps:

    1. Lookup the current precision.
    2. Lookup the current scale.
    3. Calculate the scale change.
    4. Set the new scale

    These steps, as Clojure code:

    (def x 0.000691M) ; the input number
    (def p' 1) ; desired precision
    (def s' (+ (.scale x) p' (- (.precision x)))) ; desired new scale
    (.setScale x s' BigDecimal/ROUND_HALF_EVEN)
    ; 0.0007M
    

    I know, this is a lot of steps just to change the precision!

    Why doesn't BigDecimal already provide this? Did I overlook something?

    0 讨论(0)
  • 2020-11-28 13:55

    You can use setScale() e.g.

    double d = ...
    BigDecimal db = new BigDecimal(d).setScale(12, BigDecimal.ROUND_HALF_UP);
    
    0 讨论(0)
提交回复
热议问题