Two percent in C # (turn)

Source: Internet
Author: User


The percentage after this method gets too many decimals, no
Double percent=convert.todouble (2)/convert.todouble (34);

String result= (percent*100). ToString () + "%";//Get 5.8823529411764%


This method can get the number of decimal places you want
Double percent=convert.todouble (2)/convert.todouble (34);
String result=string. Format ("{0:0.00%}", percent);//Get 5.88%

String result=string. Format ("{0:0.0000%}", percent);//Get 5.8824%


This is the ideal way to get the number of decimal places you want.
Double percent=convert.todouble (2)/convert.todouble (34);
String result=percent. ToString ("0%");//Get 6%

String result=percent. ToString ("0%");//Get 5.882%


The parameter P in this method tostring ("P") retains the two digits after the decimal point by default, which also gives you the ideal number double percent=convert.todouble (2)/convert.todouble (34);
String result=percent. ToString ("P");//Can reach 5.88%
String result =percent. ToString ("P2");//Get 5.88%

String result=percent. ToString ("P3");//Get 5.882%


Math.Round (num1,num2) method, parameter one (NUM1) is the number used for rounding, and parameter two (NUM2) is the number of digits that represent the number after the decimal point is reserved
Double Percent=math.round (2*1.00/34*100.0,4);
String result=percent. ToString () + "%";//Get 5.8824%


Transferred from http://blog.sina.com.cn/s/blog_661beca00100subx.html

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.