C # retains two digits after decimal point (method Summary)

Source: Internet
Author: User

The simplest use:

float i=1.6667f;

String Show=i.tostring ("0.00"); Result 1.67 (rounded)

Other similar methods:

String Show=i.tostring ("F");//"F2", "F" is case insensitive

String Show=string.format ("{0:f}", i);//can also be F2, or "{0:0.00}

Float J=math.round (i,2);

String show=j.tostring (); Result is 1.67

Decimal. Round (decimal. Parse (i), 2); Results 1.67

System.Globalization.NumberFormatInfo NFI = new System.Globalization.NumberFormatInfo ();

Nfi. numberdecimaldigits=2;
String result=i.tostring ("N", NFI);//Result 1.67

All of the above methods are rounded, and below are the direct truncation:

float i = 32.16667F;

int j = (int) (i * 100);

I= J * 0.01f;//Results 32.16

C # retains two digits after decimal point (method Summary)

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.