BigDecimal precision

In this chapter you will learn:

  1. Methods to control BigDecimal's precision

Methods to control BigDecimal's precision

  • int precision() Returns the precision.
  • int scale() Returns the scale.
  • BigDecimal setScale(int newScale) Change the scale.
  • BigDecimal setScale(int newScale, int roundingMode) Set scale with rounding Mode.
  • BigDecimal setScale(int newScale, RoundingMode roundingMode) Set scale with rounding Mode.
  • int signum() Returns the signum function of this BigDecimal.
  • BigInteger unscaledValue() Get the unscaled value.
  • BigDecimal ulp() Returns the size of an ulp(a unit in the last place).
import java.math.BigDecimal;
 // j  av a  2s  .c o  m

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(10f);
        System.out.println(first);
        System.out.println(first.precision());
        System.out.println(first.setScale(3));
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to remove trailing zeros
Home » Java Tutorial » BigDecimal BigInteger

BigDecimal

    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format

BigInteger

    BigInteger class
    BigInteger creation
    BigInteger add, subtract, multiply and divide
    BigInteger power and modPow
    BigInteger conversion
    BigInteger to String
    BigInteger bit and,or,xor,test,flip,negate
    BigInteger bit shift left and right
    BigInteger prime value
    BigDecimal
    BigDecimal constants
    BigDecimal Rounding mode
    BigDecimal creation
    BigDecimal calculation
    BigDecimal convert
    BigDecimal Comparison
    BigDecimal to String
    BigDecimal decimal point
    BigDecimal precision
    BigDecimal format