Java Big Data processing class BigDecimal parsing

Source: Internet
Author: User

These two days, because my required probability theory often used in the calculation of permutations and combinations, feeling very troublesome, coupled with the modern smartphone calculator is not the function of this.

So, I wrote an android's permutation combination calculator, with a day, found that there are a lot of problems, factorial up to company aims Yangzhou other numbers , the program will no longer run.

I'm here to give an example, 30 factorial, 30! =1X2X3X4X5....X30, this is clearly beyond the int range.

Thought, try to long the type bar, and later found that the 8-byte long is a drop in the bucket.

Helpless to help Baidu, found a good thing, BigDecimal class.

Viewed a number of related blog posts, found that all TM is copied and pasted documents, I was served, no that is personal opinion, direct copy document

Demo example is not clear enough to see the real egg pain, from beginning to end, just say how it is dripping, usage introduction inability to spit groove.

Do not believe, direct Baidu to see, and then back to compare my explanation here. The following is a full code comment.

The example will not raise too much, but, I promise, understand this one, the other absolutely will use.

1View.onclicklistener count =NewView.onclicklistener () {2 @Override3          Public voidOnClick (View v) {4             //M1, N1 used to directly from sring to int to judge the legitimacy of the input, do not operate5             intm1,n1;6             //BigDecimal has a number of initialization methods, I'll enumerate one here, when initializing a class object, passing in a numeric string, at which point the object value is that number7             //the following m, n are all 1, because my operation of this listener event is starting from 1. 8 9BigDecimal m =NewBigDecimal ("1");TenBigDecimal n =NewBigDecimal ("1"); One  ABigDecimal result_2 =NewBigDecimal ("1");//This is used to save m!. /(M-n)!n! The m-n factorial in is starting from 1 -Toast.maketext (myactivity. This, save_m+"***"+save_switch+"///"+Save_n,toast.length_long). Show (); -  theM1 = Integer.parseint (save_m);//Save_m and the save_n below are all editText inputs get the value -N1 =Integer.parseint (save_n); -             if(m1<0|| n1<0|| M1 <N1) { -T3.settext ("keep m>=0,n>=0, and M >n, combine calculation please keep m<900,n<900"); +}Else { -                 //It 's too big, there's a delay in the operation, hundreds of millions of counts, and the computer takes a few seconds . +                 if(Save_switch.equals ("Combination")  || N1> the|| M1> the) { AT3.settext ("combination calculation please keep m<900,n<900"); at}Else { -                     if(Save_switch.equals ("Combination")) { -                         //Long result_1 = 1, result_2 = 1, result = 1;//Long is not enough . -                          for(inti =1; I <= M1; i++) { -LOG.D ("Tag","before * M is"+ M +":"+"I is"+i); -  in                             //BigDecimal class method, multiplication, where the multiplication includes the same as the addition, subtraction, and addition of the same, no longer is + 、-、 *,/ -                             //Common multiplication: Multiply (BigDecimal xx), four have a return value, return type is also BigDecimal to                             //The following line is an example +m = m.multiply (bigdecimal.valueof (i));//Multiply I per -                             //bigdecimal.valueof above Me (i) this is used to convert an int type I into a BigDecimal object the  *                             if(I <=N1) { $LOG.D ("Tag","before * n is"+ N +":"+"I is"+i);Panax Notoginsengn =n.multiply (bigdecimal.valueof (i)); -                             } the                             if(I <= (M1-N1)) { +LOG.D ("Tag","before * result_2 is"+ Result_2 +":"+"I is"+i); AResult_2 =result_2.multiply (bigdecimal.valueof (i)); the                             } +LOG.D ("Tag","-"+result_2); -                         } $LOG.D ("Tag","m->"+m); $LOG.D ("Tag","n->"+n); -LOG.D ("Tag","r->"+result_2); -                         //Division: Divide (), usage reference above multiplication them =m.divide (n.multiply (result_2)); -T3.settext ("Result is:"+ M +";");Wuyi                     } the                 } -                 if(Save_switch.equals ("Arrange")){ WuToast.maketext (myactivity. This,"789798", Toast.length_long). Show (); -                     //int result_1 = 1, result_2 = 1, result = 1; About                      for(inti =1; I <= M1; i++) { $                        //log.d ("tag", "is->" +result); -m =m.multiply (bigdecimal.valueof (i)); -LOG.D ("Tag","i->"+i); -                         if(I <= (M1-N1)) { AResult_2 =result_2.multiply (bigdecimal.valueof (i)); +                         } the                     } -                     //T3.settext ("for double was:" + result + ";" + "for Fenshu is:" + result_2); $m =m.divide (result_2); theT3.settext ("Result is:"+ M +";"); the                 } the             } the         } -};

BigDecimal can be counted to more than 70, possibly more, decimal oh.

Java Big Data processing class BigDecimal parsing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.