Re: console.write/math problems-just learning

  • From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 20 Sep 2007 15:02:49 +0100

Hi Ty,

 I've tweaked your code in the light of your questions.  To find out about
formatting, look in the VS Help system for format, number, then be prepared
to follow several likely looking links to related pages.  Your thoughts were
correct as far as they went.  The first figure in the braces is the
parameter number.  The second gives the total field width in characters.
That figure can then be followed by a colon and one of a number of
characters (d for integer, f for float etc), and the figure immediately
following that gives either the number of significant figures (for the
integer), or the number of decimal places (for the float).

 You should also note that the memory sizes are returned as longs, so when
dividing by 1024, you were doing integer arithmetic.  Making one of the
divisors a float gives an answer that isn't just integral.

 Here is the modified code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace tyler1
{
 class Program
 {
   static void Main(string[] args)
   {
     DriveInfo[] drives = DriveInfo.GetDrives();
     foreach (DriveInfo d in drives)
     {
       Console.WriteLine("Drive {0}", d.Name);
       Console.WriteLine(" File type: {0}", d.DriveType);

       if (d.IsReady == true)
       {
         Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
         Console.WriteLine(" File system: {0}", d.DriveFormat);
         Console.WriteLine(
           " Available space to current user: {0, 6:f2} MB",
           (d.AvailableFreeSpace/1024/1024.0));
         Console.WriteLine(
           " Total available space: {0, 6:f2} MB",
           (d.TotalFreeSpace/1024/1024.0));
         Console.WriteLine(" Total size of drive: {0, 2} bytes ",
d.TotalSize);

         double percent = (100.0 * d.TotalFreeSpace / d.TotalSize);
         Console.Write (" Percent available: {0, 2:f2}%\n\n", percent);
         Console.ReadKey();
       }
     }
   }
 }
}

 hth

Martin

----- Original Message ----- From: "Littlefield, Tyler" <compgeek13@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, September 16, 2007 2:29 AM
Subject: c#: console.write/math problems-just learning


Hello list,
I'm in the middle of learning c#.. yay! :)
I'm having problems with some code:
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

DriveInfo[] drives = DriveInfo.GetDrives();

foreach (DriveInfo d in drives)

{

Console.WriteLine("Drive {0}", d.Name);

Console.WriteLine(" File type: {0}", d.DriveType);

if (d.IsReady == true)

{

Console.WriteLine(" Volume label: {0}", d.VolumeLabel);

Console.WriteLine(" File system: {0}", d.DriveFormat);

Console.WriteLine(

" Available space to current user:{0, 15} mb",

(d.AvailableFreeSpace/1024/1024));

Console.WriteLine(

" Total available space: {0, 15} gb",

(d.TotalFreeSpace/1024/1024));

Console.WriteLine(

" Total size of drive: {0,2} bytes ",

d.TotalSize);

double percent = ((d.TotalFreeSpace / d.TotalSize) );

Console.Write(percent);

Console.Write ("Percent available: ", percent);

Console.ReadKey();

}

}

}

}

}

The problem is: It's not really doing the math, (according to
Console.Write(percent).

Also, can someone explain the formatting?

I've used {0, 15} from sample code, but not sure what it means.

I was told that the first elemtn 0 refered to the parameter, which I'm
assuming would be my string, and 15 refers to the type of number to print.
15.00 etc... But why 15? :)

Thanks lots for the help/pointers, really appriciated.

Thanks,
Tyler Littlefield.

---snip---

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: