• 0

C#


Question

okay c# programmers...have a stupid question to ask of you...how do i specify the number of decimal places for a double/decimal value??

in C++ it's cout.precision(*no. of digits*)...but can't seem to find something like this for C#...please help :)

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You could do something like this:

<pre>

double dblPi = 22/7f;



Console.WriteLine("pi = {0}", dblPi);

Console.WriteLine("pi to 10 Decimal Places = {0:F10}", dblPi);

Console.WriteLine("pi to 5 Decimal Places = {0:F5}", dblPi);

Console.WriteLine("pi to 2 Decimal Places = {0:F2}", dblPi);

Console.WriteLine("pi to 0 Decimal Places = {0:F0}", dblPi);

</pre>

Would produce output, like this:

<pre>

pi = 3.14285714285714

pi to 10 Decimal Places = 3.1428571429

pi to 5 Decimal Places = 3.14286

pi to 2 Decimal Places = 3.14

pi to 0 Decimal Places = 3

</pre>

Link to comment
Share on other sites

  • 0

To quote from the MSDN

A class is a template or blueprint that defines the characteristics of an object and describes how the object should look and behave.

A namespace is a logical unit for grouping classes and class instances, and for controlling their scope and visibility.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.