How to validate max and min of Long and BigDecimal data type
1. Hi, I need to read the input from the user and use Long.parseLong() to parse it to long datatype.
However, before parsing, I wish to check whether the user has entered the value greater than the max value of long (2^63-1) or smaller than min value of long(-2^63), how to do this?
2. The same question go to BigDecimal. By the way, what is the max and min value for BigDecimal?
[398 byte] By [
williameea] at [2007-11-26 20:19:14]

1.) Try what happens when you try to call Long.valueOf() on a String that represents a number bigger or smaller than what long can handle. That will guide you to the correct solution.
2.) There is no maximum value for BigDecimal, it's only limited by the available memory (*). So no need for #2.
(*) to be perfectly honest, there probably is a maximum value, that could pop up if you've got unearthly amounts of memory, but I doubt it'll matter in your case.
Thanks JoachimSauer..
Hi, I think my question is not precise enough, so far only JoachimSauer got the point...
My idea is, before we can compare them with the max value, we need to store in a bigger container for number, since long and bigdecimal may be the largest for integer and floating point value, hence we need to find some alternative ways for doing this:
1)One way I can think of is store it in the string and compare the length...but seems to be not precise enough.
2) As JoachimSauer metioned, observe the exception...
> Thanks JoachimSauer..
> Hi, I think my question is not precise enough, so far
> only JoachimSauer got the point...
>
> My idea is, before we can compare them with the max
> value, we need to store in a bigger container for
> number, since long and bigdecimal may be the largest
> for integer and floating point value, hence we need
> to find some alternative ways for doing this:
> 1)One way I can think of is store it in the string
> and compare the length...but seems to be not precise
> enough.
That will not work.
> 2) As JoachimSauer metioned, observe the exception...
Yes. Why not just do that? That's the simplest, and, I'd argue, the correct way.