C # High-Precision addition supports decimals (to be optimized ),

Source: Internet
Author: User

C # High-Precision addition supports decimals (to be optimized ),

Directly Add code

Implementation ideas:

1. First fill the decimal point, 9223372036854775808.9 + 9223372036854775808.9223372036854775808 => 9223372036854775808.900000000000000000 + 9223372036854775808.9223372036854775808

2. Start to calculate by bit. Place the number of incoming bits into jw and add it to the next bits.

Public static char [] Sum (string a, string B) {return Sum (. toCharArray (), B. toCharArray ();} public static char [] Sum (char [] a, char [] B) {char [] c =; // a char [] d = B after the completion; // The B int alength = a after the completion. count (); // a length int blength = B. count (); // B length int adot = alength; // a decimal point int bdot = blength; // B decimal point # region decimal point complement for (int I = 0; I <alength; I ++) {if (a [I] = '. ') {adot = I; break;} for (int I = 0; I <blength; I ++) {if (B [I] = '.') {bdot = I; break ;}} if (adot! = Alength | bdot! = Blength) {if (adot = alength) {c = new char [alength + 1 + blength-bdot-1]; for (int I = 0; I <c. length; I ++) {if (I <alength) {c [I] = a [I];} else if (I = alength) {c [I] = '. ';} else {c [I] = '0' ;}} else if (bdot = blength) {d = new char [blength + 1 + alength-adot-1]; for (int I = 0; I <d. length; I ++) {if (I <blength) {d [I] = B [I];} else if (I = blength) {d [I] = '. ';} else {d [I] = '0' ;}} else {if (alength-adot> blength-bdot) {d = new char [blength + (alength-adot)-(blength-bdot)]; for (int I = 0; I <d. length; I ++) {if (I <blength) {d [I] = B [I];} else {d [I] = '0 ';}}} else {c = new char [alength + (blength-bdot)-(alength-adot)]; for (int I = 0; I <c. length; I ++) {if (I <alength) {c [I] = a [I];} else {c [I] = '0' ;}}}# endregion List <char> item = new List <char> (); int cl = c. length; int dl = d. length; int r = 0; int jw = 0; // carry do {if (cl> 0 & dl> 0) {if (c [cl-1] = '. ') {r = '. ';} else {r = (int) c [cl-1] + (int) d [dl-1]-96 + jw; jw = 0; if (r >=10) {jw ++; r = r-10 ;}} else if (cl <= 0 & dl> 0) {r = d [dl-1]-48 + jw; jw = 0;
If (r> = 10)
{
Jw ++;
R = r-10;
}
} Else if (cl> 0 & dl <= 0) {r = c [cl-1]-48 + jw;
Jw = 0;
If (r> = 10)
{
Jw ++;
R = r-10;
} If (r = 46) {item. add (Convert. toChar (r);} else {item. add (Convert. toChar (r. toString ();} cl --; dl --;} while (cl> 0 | dl> 0); if (jw = 1) {item. add ('1');} return item. toArray ();}

 

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.