Avatar of william007
william007

asked on 

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?

JavaJava EE

Avatar of undefined
Last Comment
CEHJ
Avatar of mnrz
mnrz

use Long.MAX_VALUE
Avatar of mnrz
mnrz

by the way, about BigDecimal:
the BigDecimal includes an unscaled value as Integer and a 32-bit integer scale.
it means the max value can check with Integer.MAX_VALUE for the left side of the point.
Avatar of william007
william007

ASKER

Thanks.
However before you can compare, you need to store in a bigger container for number.(long and bigdecimal may be the largest for integer and floating point value).
The other way I can think of is store it in the string and compare the length...but seems to be not precise enough.
Avatar of mnrz
mnrz

the BigDecimal will store the number in scale. you can read its javadoc.
it needs to specify the scale for more precise results.

here is example from the javadoc:
19/100 = 0.19 // integer=19, scale=2
but
21/110 = 0.190 // integer=190, scale=3

and some comments:
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Jaax
Jaax
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of william007
william007

ASKER

Thanks:)
Avatar of Jaax
Jaax
Flag of India image

Hi william007,
 I think I deserve to be acknowledged at least as a provider of "Assisted Solution"
 - The pattern matching technique was first suggested by me
- I think my solution is as good as the "Accepted solution" as once the string is matched to be a long, the only reason it can fail a Long.parseLong is when its value is beyond the Long's range. Therefore it is unnecessary to convert to BigDecimal and check the bounds.
Avatar of william007
william007

ASKER

Hi Jaax,

Thanks for taking my attention to your answer.

I do not grade your answer initially because your approach is kind of implicit, hence this lead me to think that there are many circumstances that may make the JVM throw exception. However, take a closer analysis of your code, now I am convinced that only the max and min value will make it throw an exception.

Btw, I will still grant CEHJ answer as "accepted solution" because he has directly and explicitly transfer my question

"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?"

into this

" if ((BigInteger.valueOf(Long.MAX_VALUE)).compareTo(bi) >= 0 &&
 (BigInteger.valueOf(Long.MIN_VALUE)).compareTo(bi) <= 0)"

I will reopen and regrade the question.

All the best:)

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>the only reason it can fail a Long.parseLong is when its value is beyond the Long's range.

That's not the case - there are many possible inputs that can make Long.parseLong fail

>> Therefore it is unnecessary to convert to BigDecimal and check the bounds.

It's simply better to know more about the problem than less so that the user can correct the input. In fact, my code could be improved by
a. specifying which bound had been exceeded
b. specifying the bounds of long
Avatar of william007
william007

ASKER

>>That's not the case - there are many possible inputs that can make Long.parseLong fail
Hi CEHJ, since Jaax has checked for the numeric expression before parsing, I can't think of any cases that will make it throw NumberFormatException with the message (..." is beyong the range supported by the long primitive").
can you give an example of the input that will make Jaax code failed other than
"the value greater than the max value of long (2^63-1) or smaller than min value of long(-2^63)"?
If there is any(One example is enough), I will maintain my original grading decision, and we can learn some new knowledge from this as well:)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Hi CEHJ, since Jaax has checked for the numeric expression

Sorry - i was forgetting this. btw, my comments were not by way of reinforcing the original grading - it's only fair that Jaax is included as well
Avatar of william007
william007

ASKER

I see, no prob:)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo