Effect:
Description: enter a decimal number and the number of digits to be retained,
Event: click button
Code:
1 Public Static Double Round ( Double D, Int I) 2 { 3 If (D> = 0 ) 4 { 5 D + = 5 * Math. Pow ( 10 ,-(I + 1 )); // Evaluate the specified power of a specified number of times 6 } 7 Else 8 { 9 D + = 5 * Math. Pow ( 10 ,-(I + 1 )); 10 } 11 String STR = D. tostring (); 12 String [] STRs = Str. Split ( ' . ' ); 13 Int Idot = Str. indexof ( ' . ' ); 14 String Prestr = STRs [ 0 ]; 15 String Poststr = STRs [ 1 ]; 16 If (Poststr. length> I) 17 { 18 Poststr = Str. substring (idot + 1 , I ); // Number of digits required for Truncation 19 } 20 If (Poststr. Length <= 2 ) 21 { 22 Poststr = poststr +" 0 " ; 23 } 24 String Strd = prestr + " . " + Poststr; 25 D = Double . Parse (strd ); // Converts a string to a double-precision real number. 26 Return D; 27 } 28 29 Private Void Button#click ( Object Sender, eventargs E) 30 { 31 Textbox3.text =Convert. tostring (math. Round (convert. todouble (textbox1.text. Trim (), convert. toint16 (textbox2.text. Trim ()))); 32 }