As with the numeric conversions you also have the

Info icon This preview shows pages 359–361. Sign up to view the full content.

View Full Document Right Arrow Icon
As with the numeric conversions, you also have the option of custom format strings. The key components are: d : day M : month (note that this is uppercase to distinguish it from m for minute) y : year h : hour (12-hour format) H : hour (24-hour format) m : minute s : seconds f : fractions of a second The / character will be substituted with the appropriate date separator for your locale, and : with the time separator. You can repeat the substitution character to obtain shorter/longer forms of the relevant part of the date or time. For example, you can format the day part like Example 10-38 does. Example 10-38. Formatting the day DateTime time = new DateTime(2001, 12, 24, 13, 14, 15, 16); Console.WriteLine(time.ToString("dddd")); Console.WriteLine(time.ToString("ddd")); Console.WriteLine(time.ToString("dd")); This will produce: Monday Mon 24 (As you saw in Example 10-33 , a single d means something else: it shows the whole date, in short form.) Other useful formatting characters include: z : offset from UTC (with zzz providing hours and minutes) tt : the a.m./p.m. designator As with the numeric formats, you can also include string literals, escaping special char- acters in the usual way. Formatting Data for Output | 335
Image of page 359

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Going the Other Way: Converting Strings to Other Types Now that we know how to control the formatting of various types when we convert them to a string, let’s take a step aside for a moment to look at converting back. If we’ve got a string, how do we convert that to a numeric type, for instance? Probably the easiest way is to use the static methods on the Convert class, as Exam- ple 10-39 shows. Example 10-39. Converting a string to an int int converted = Convert.ToInt32("35"); This class also supports numeric conversions from a variety of different bases (specif- ically 2, 8, 10, and 16), shown in Example 10-40 . Example 10-40. Converting hexadecimal strings to ints int converted = Convert.ToInt32("35", 16); int converted = Convert.ToInt32("0xFF", 16); Although we get to specify the base as a number, only binary, octal, decimal, and hex- adecimal are actually supported. If you request any other base (e.g., 7) the method will throw an ArgumentException . What happens if we pass a string that doesn’t represent an instance of the type to which we want to convert, as Example 10-41 does? Example 10-41. Attempting to convert a nonnumeric string to a number double converted = Convert.ToDouble("Well, what do you think?"); As this string cannot be converted to a double, we see a FormatException . Throwing (and catching) exceptions is a relatively expensive operation, and sometimes we want to try a particular conversion, then, if it fails, try another. We’d rather not pay for the exception if we don’t have to. Fortunately, the individual numeric types (and DateTime ) give us the means to do this. Instead of using Convert , we can use the various TryParse methods they provide.
Image of page 360
Image of page 361
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern