• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the requirement and purpose?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would you advise this method, if i am passing the String parameter-

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

 
Carl E Knoettner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!