java - Big Decimal while loop -


i had while loop used integer converted big decimal since the value high , i'm unsure of how make while loop work big decimal instead of integer code follows.

public string getsize() {   bigdecimal size = new  bigdecimal(mysize);   int count = 0;   string datatype = ""; while (size > 1000 )   {       size = size.divide(size, 1000);    count++;   }    switch (count)   {     case 0:     datatype = "bytes";     break;     case 1:     datatype = "kb";     break;     case 2:     datatype = "mb";     break;     case 3:     datatype = "gb";     break;     case 4:     datatype = "kb";     break;       }        return size + datatype ; }  

the error @ line while (size > 1000 ) since method if size integer. can tell me method using big decimal size? i've had trouble coming it
edit: solutions given , unrelated problem popped in division line im getting: "exception in thread "main" java.lang.illegalargumentexception: invalid rounding mode" idea of how fix this? figured better post here make entire new question if that's whats proper ill that.

to compare bigdecimals, use compareto method; bigdecimal comparable. compareto method return integer greater 0 if size greater thousand.

bigdecimal thousand = new bigdecimal(1000); while (size.compareto(thousand) > 0 ) 

incidentally, doesn't doing bigdecimal division correctly:

size = size.divide(size, 1000); 

that interpret 1000 rounding mode , result in illegalargumentexception. think need divide it:

size = size.divide(thousand);  // see above bigdecimal "thousand" declaration 

additionally, case 4 incorrect; should "tb" terabytes after dividing 1000 4 times (trillions):

case 4: datatype = "tb"; break;  

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -