BigDecimal - material decimal places
I have a BigDecimal value and I want to know if the number of decimal places it has is outside a boundary.
e.g. If BigDecimal is 123.456 and my decimal places boundary is 2 then this would be an error
However 123.450 with a boundary of 2 is ok as I consider the 0 to be immaterial for this test.
I am a bit unsure about BigDecimal with it's scale/unscaled implementation.
I have looked at scale()
as an option but I think this could be wrong 开发者_开发问答for my case where trailing zeros are concerned.
Does anyone know the correct way to test for this?
BigDecimal
has stripTrailingZeros()
. After that scale()
will do what you want.
OR you could use BigDecimal.setScale(2, RoundingMode.UNNECESSARY)
which will throw an Exception if there are non-zeros. To me this looks a bit inefficient in the common case. But if you would throw an exception anyway it might be useful.
精彩评论