java - How do I validate precision and scale with BigDecimal? -


i'm trying validate precision , scale of big decimal.

i'm looking validate big decimal doesn't exceed precision of 10 or scale of 2. tried doing maxlength value wouldn't violating db length constraints, unable working either. kindly point me in direction resolving issue?

the bigdecimal class has 3 methods of interest this:

  • precision(), returns number of digits unscaled value (for instance, number 123.45, precision returned 5)
  • scale(), returns number of digits after decimal separator when positive (scale() can negative, in case, unscaled value of number multiplied ten power of negation of scale. example, scale of -3 means unscaled value multiplied 1000),
  • striptrailingzeros(), returns bigdecimal trailing zeros removed representation.

for instance, compute precision , scale given bigdecimal b, can write that:

bigdecimal nozero = b.striptrailingzeros(); int scale = nozero.scale(); int precision = nozero.precision();     if (scale < 0) { // adjust negative scale     precision -= scale;     scale = 0;         } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -