Formatting data for output 331 quoting a custom

Info icon This preview shows pages 355–358. Sign up to view the full content.

View Full Document Right Arrow Icon
Formatting Data for Output | 331
Image of page 355

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

View Full Document Right Arrow Icon
Example 10-29. @-quoting a custom format string int value = 12345678; string text = value.ToString( @ "###-### \# ###"); Console.WriteLine(text); Both will produce this output: 12-345 # 678 You can also include literal strings (with or without special characters), by wrapping them in single quotes as Example 10-30 shows. Example 10-30. Literal string in a custom format string int value = 12345678; string text = value.ToString( @ "###-### \# ### 'is a number'"); Console.WriteLine(text); This produces the output: 12-345 # 678 is a number Finally, you can also get the multiply-by-100 behavior for predivided percentage values using the % symbol, as shown in Example 10-31 . Example 10-31. Percentage in a custom format string double value = 0.95; string text = value.ToString("#0.##%"); Console.WriteLine(text); Notice that this also includes the percentage symbol in the output: 95% There is also a per-thousand (per-mille) symbol ( ), which is Unicode character 2030. You can use this in the same way as the percentage symbol, but it multiplies up by 1,000. We’ll learn more about Unicode characters later in this chapter. Dates and Times It is not just numeric types that support formatting when they are converted to strings. The DateTime , DateTimeOffset , and TimeSpan types follow a similar pattern. DateTimeOffset is generally the preferred way to represent a particular point in time inside a program, because it builds in information about the time zone (and daylight saving if applicable), leaving no scope for ambiguity regarding the time it represents. However, DateTime is a more natural way to present times to users, partly because it has more scope for ambiguity. People very rarely explicitly say what time zone they’re thinking of—we’re used to learning that a shop opens at 9:00 a.m., or that our flight 332 | Chapter 10: Strings
Image of page 356
is due to arrive at 8:30 p.m. DateTime lives in this same slightly fuzzy world, where 9:00 a.m. is, in some sense, the same time before and after daylight saving comes into effect. So if you have a DateTimeOffset that you wish to display, unless you want to show the time zone information in the user interface, you will most likely convert it to a DateTime that’s relative to the local time zone, as Example 10-32 shows. Example 10-32. Preparing to present a DateTimeOffset to the user DateTimeOffset tmo = GetTimeFromSomewhere(); DateTime localDateTime = tmo.ToLocalTime().DateTime; There are two benefits to this. First, this gets the time into a representation likely to align with how end users normally think of times, that is, relative to whatever time zone they’re in right now. Second, DateTime makes formatting slightly easier than DateTimeOffset : DateTimeOffset supports the same ToString formats as DateTime , but DateTime offers some additional convenient methods. First, DateTime offers an overload of the ToString method which can accept a range of standard format strings. Some of the more popular ones (such as d , the short date format, and D , the long date format) are also exposed as methods. Example 10-33 il- lustrates this.
Image of page 357

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

View Full Document Right Arrow Icon
Image of page 358
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