Forums Register Login

how to check whether BigDecimal or String is whole number or not ?

+Pie Number of slices to send: Send
Well i did this code for BigDecimal, altougth i am also open to do the same thing for String input also.



Well my second method works more accurate.

Any advise or new approach to be followed ?

i think in my production environment it is not possible to have such a big scale number. but you know if it will not happen, i have perception in my mind, be safe. this thing always remains there and pinch me.
+Pie Number of slices to send: Send
What is the requirement and purpose?
+Pie Number of slices to send: Send
Using doubleValue() is a major flaw - double has limited precision, so if the number becomes too large the least significant digits are simply dropped.

Instead, use BigDecimal's toPlainString() method and check if it either has no ., or ends with . followed by multiple zeros. A regular expression can help you:

I would have said "round you BigDecimal and see if it is equal", but BigDecimal actually makes a difference between "12" and "12.0".
+Pie Number of slices to send: Send
how would you advise this method, if i am passing the String parameter-

+Pie Number of slices to send: Send
How about "12.0"? Is that not a whole number?

The regular expression I posted handles this case. It basically means: any number of digits, optionally followed by a dot and any number of zeros. You can prepend "-?" to allow negative numbers as well.
+Pie Number of slices to send: Send
 

Rob Prime wrote:How about "12.0"? Is that not a whole number?

The regular expression I posted handles this case. It basically means: any number of digits, optionally followed by a dot and any number of zeros. You can prepend "-?" to allow negative numbers as well.



Even though for negative numbers it is not required, still i will add it.

So the final code, i will be using it is :

+Pie Number of slices to send: Send
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
// This code is much easier to understand and is less error prone.
public static void main(String[]args){
String str = "1.00000000000000000001";
BigDecimal bd = new BigDecimal(str);
System.out.println(isWhole(bd));
System.out.println(isWholeString(str));

str = "12.0";
bd = new BigDecimal(str);
System.out.println(isWhole(bd));
System.out.println(isWholeString(str));

str = "-1.0000000000000000001";
bd = new BigDecimal(str);
System.out.println(isWhole(bd));
System.out.println(isWholeString(str));
}
// This method is for BigDecimal input
public static boolean isWhole(BigDecimal bd){
String bistr = bd.toBigInteger().toString();
String bdstr = bd.setScale(0, RoundingMode.UP).toString();
return bistr.equals(bdstr);
}
// this method is for String input
public static boolean isWholeString(String str){
BigDecimal bd = new BigDecimal(str);
return isWhole(bd);
}
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 20891 times.
Similar Threads
BigDecimal rounding problem
Converting BigDecimal to long with rounding
Trouble with double primitive type
Checking for zero value ?
multithreading not working correctly
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:41:15.