WinForm Small program------Keep n-bit rounding decimals

Source: Internet
Author: User

One: You can choose to retain the number of digits, the comments are explained very detailed,

Two: All code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceround{ Public Partial classFrm_main:form { PublicFrm_main () {InitializeComponent (); }        Private voidBtn_get_click (Objectsender, EventArgs e) {            DoubleP_dbl_d1, P_DBL_D2;//define two variables of type double            if(Double. TryParse (TXT_ADD1. Text, outP_DBL_D1) &&//determine if the input information is correct                Double. TryParse (TXT_ADD2. Text, outp_dbl_d2)) {txt_add3. Text=//The value after rounding is obtainedRound (p_dbl_d1 + p_dbl_d2, Cbox_select. SelectedIndex +1).            ToString (); }            Else{MessageBox.Show (//Prompt for error messages                    "The input value is not correct! ","Tips! "); }        }        /// <summary>        ///method for calculating the rounding of double values/// </summary>        /// <param name= "dbl" >numeric value for rounding of calculated results</param>        /// <param name= "I" >Reserved decimal digits</param>        /// <returns>returns the double value after rounding</returns>        Internal DoubleRound (DoubleDblinti) {stringP_str_dbl = Dbl. ToString ();//converts a double value to a string            stringP_str_lower =//converts a double numeric decimal bit to a stringP_str_dbl. Split ('.')[1];//----------get '. ' After the character, (this usage on the net has little relevant material)            intP_str_length = P_str_lower. Length;//To calculate a double numeric decimal digit lengthDbl+ = GetValue (i, p_str_length);//to start rounding operationsP_str_dbl = Dbl. ToString ();//converts the value after an operation to a string            if(P_str_dbl. Contains ("."))//determine if there are decimal digits            {                stringP_str_upper =//To get an integer digit stringP_str_dbl. Split ('.')[0];//----------get '. ' The previous characterP_str_lower = P_str_dbl. Split ('.')[1];//get the decimal digit string                if(P_str_lower. Length > I)//determine if the number of digits is greater than the number of reserved decimal digits{p_str_lower= P_str_lower. Substring (//intercept reserved decimal digits                        0, i); return Double. Parse (//returns a Double valueP_str_upper +"."+p_str_lower); }                Else                {                    return Double. Parse (P_STR_DBL);//returns directly if the number of digits is less than the number of reserved decimal digits                }            }            Else            {                return Double. Parse (P_STR_DBL);//return the value directly if there are no decimal digits            }        }        /// <summary>        ///returns a decimal number for the next digit of 5, such as 4 decimal places, that is required to be retained, i.e. 0.0005/// </summary>        /// <param name= "Int_null" >rounding a reserved number of digits</param>        /// <param name= "Length" >Number of decimal digits of the original data</param>        /// <returns>returns a decimal value for rounding calculations</returns>        Internal DoubleGetValue (intInt_null,intlength) {            stringP_str_dbl ="0.";//define a string variable and assign a value             for(inti =0; i < length; i++)//adding decimal digits with A for loop            {                if(I > Int_null-1) {P_str_dbl+="5";//Add 5 to the rounding portion of the decimal                }                Else{P_str_dbl+="0";//add 0 to the reserved portion of the decimal                }            }            return Double. Parse (P_STR_DBL);//returns the decimal value        }        Private voidFrm_main_load (Objectsender, EventArgs e) {Cbox_select. SelectedIndex=0;//Cbox_select Control selects the first item by default        }    }}

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.