Number of decimal digits for C # custom percent values

Source: Internet
Author: User

This tip describes how to use the Percentdecimaldigits property of the NumberFormatInfo class to set the number of digits reserved after the decimal point in the percent value.

Percentdecimaldigits Property

Use this property to get or set the number of decimal places to use in percent values.

The syntax format is:

public int PercentDecimalDigits{get;set;}

Property value:-The number of decimal places to use in percent values. The default value is 2.

The main code is as follows:

private void button1_Click(objectsender,EventArgse)
{
System.Globalization.NumberFormatInfoGN=new System.Globalization.CultureInfo("zh-CN",false).NumberFormat;
Double myInt=0.9774;
string strInfo=null;
GN.PercentDecimalDigits=1;
strInfo+="保留一位小数:"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=2;
strInfo+="n保留二位小数:"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=4;
strInfo+="n保留四位小数:"+string.Format(myInt.ToString("P",GN));
GN.PercentDecimalDigits=6;
strInfo+="n保留六位小数:"+string.Format(myInt.ToString("P",GN));
MessageBox.Show(strInfo,"定义效果");
}

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.