Double Type Precision Control of c #

Source: Internet
Author: User

Double Type Precision Control of c #
C # decimal precision

In c #, if you want to retain a certain degree of precision for decimal places of the double type decimal places, you can use parameter restrictions when converting them into strings. The following program demonstrates this practice.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace double Precision {// calculate the average value of an integer array, retain two decimal places; class Program {static void Main (string [] args) {int [] nums = {1, 2, 3, 4, 5, 6, 9}; double avg = nums. average (); Console. writeLine (avg); Console. writeLine ("{0: 0. 00} ", avg); string str = avg. toString ("0.00"); Console. writeLine (str); Console. readKey ();}}}

In fact, both the ToString and the standardized output are converted to a string first. Therefore, when Console. WriteLine () calls ToString, it will pass the value following the colon as a parameter.
It is equivalent to Console. WriteLine (avg. ToString (0.00 ));

Running result

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.