We get this behavior when we put a comma between a

Info icon This preview shows pages 354–356. Sign up to view the full content.

millions, billions, etc.). We get this behavior when we put a comma between a couple of digit placeholders (the placeholders being either # or 0 ), as Example 10-24 shows. Example 10-24. Comma for grouping digits int value = 12345678; string text = value.ToString("#,#"); Console.WriteLine(text); Our output string now looks like this: 12,345,678 On the other hand, commas placed just to the left of the decimal point act as a scale on the number. Each comma divides the result by 1,000. Example 10-25 shows two commas, dividing the output by 1,000,000. (It also includes a comma for grouping, although that will not have any effect with this particular value.) Example 10-25. Comma for scaling down output int value = 12345678; string text = value.ToString("#,#,,."); Console.WriteLine(text); This produces: 12 Format strings don’t have to have a decimal point, but you can still use commas to scale the number down even when there’s no decimal point for the commas to be to the left of—they just appear at the end of the format string instead. In effect, there’s an implied decimal point right at the end of the string if you leave it off, so in Example 10-26 , the commas are still considered to be to the left of the point even though you can’t see it. 330 | Chapter 10: Strings
Image of page 354

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

Example 10-26. Implied decimal point int value = 12345678; string text = value.ToString("#,#,,"); Console.WriteLine(text); Again, this produces: 12 The division rounds the result, so 12745638 would produce 13 with the same formatting. You can also add your own arbitrary text to be included “as is” in the format string, as Example 10-27 shows. Example 10-27. Arbitrary text in a custom format string int value = 12345678; string text = value.ToString("###-### but ###"); Console.WriteLine(text); This time, the output is: 12-345 but 678 Notice how it includes the extra characters we included (the - and the but ). Were you expecting the output to be 123-456 but 78 ? The framework applies the placeholder rule for the lefthand side of the decimal point, so it drops the first nonrequired placeholder, not the last one. Remember that this is a numeric conversion, not something like a telephone-number format. The behavior may be easier to understand if you replace each # with 0 . In that case, we’d get 012-345 but 678 . Using # just loses the leading zero. If you want to include one of the special formatting characters, you can do so by es- caping it with a backslash. Don’t forget that the C# compiler will attempt to interpret backslash as an escape character in a literal string, but in this case, we don’t want that— we want to include a backslash in the string that we pass to ToString . So unless you are using the @ symbol as a literal string prefix, you’ll need to escape the escape character as Example 10-28 shows. Example 10-28. Escaping characters in a custom format string int value = 12345678; string text = value.ToString("###-### \\# ###"); Console.WriteLine(text); Example 10-29 shows the @ -quoted equivalent.
Image of page 355
Image of page 356
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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