Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 856
  • 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.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the requirement and purpose?
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 856
  • 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: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 856
  • 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 :

 
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);
}
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!