Reputation: 5794
I'm doing an example from the book: The Complete Reference C# 3.0 by Herbert Schildt. It's about writing in Console.WriteLine with arguments. Here is the example: I tried this but I was getting an error:
Project1.exe has encountered a problem and needs to be close. We are sorry for the inconvenience. Please tell Microsoft about this problem. Send Error Report or Don't Send. And if I click, I get another error in the command prompt. "Unhandled Exception: System.Format.Exception input string was not in a correct format. at System.Text.StringBuilder.AppendFormatError() at System.Text.StringBuilder.AppendFormat(IFormatProvider provider,String Format, Object[]args) at System.IO.TextWriter.WriteLine(String format, Object arg0) at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0) At Example2.Main() in D:\myPath
I'm not sure if the book has some error or is it my code? I would appreciate your help. Thanks
One of the easiest ways to specify a format is to describe a template that WriteLine( ) will use. To do this, show an example of the format that you want, using #s to mark the digit positions. You can also specify the decimal point and commas. For example, here is a better way to display 10 divided by 3:
Console.WriteLine("Here is 10/3: {0:#.##}", 10.0/3.0);
The output from this statement is shown here: Here is 10/3: 3.33
Btw this is my code looks like:
static void Main()
{
Console.WriteLine("Here is 10/3: {0:#.##)", 10);
}
Upvotes: 1
Views: 347
Reputation: 61121
Let me also post an identical answer to everybody else for no reason whatsoever!
Replace the ##) with ##} in your code.
Upvotes: 0
Reputation: 4772
Corrected code below:
static void Main()
{
Console.WriteLine("Here is 10/3: {0:#.##}", 10.0/3);
}
Upvotes: 1
Reputation: 5330
It should be :
Console.WriteLine("Here is 10/3: {0:#.##}", 10);
When you are using a format you should put it in { and }
Upvotes: 1
Reputation: 13127
Your code is wrong. You used a parenthesis instead of a curly brace in your string literal. Try this:
static void Main()
{
Console.WriteLine("Here is 10/3: {0:#.##}", 10);
}
Upvotes: 3
Reputation: 116481
Your format string is wrong. You have a {
paired with a )
Upvotes: 2
Reputation: 391664
You are using the wrong ending brace for the format parameter.
Note the end parenthesis ) after #.##, it should be a } instead (curly braces).
Also note that you have left out the division, and if you simply change your code to this (corrected the brace as well):
static void Main()
{
Console.WriteLine("Here is 10/3: {0:#.##}", 10/3);
}
Then you're going to have another question as well, since the result of that will be:
Here is 10/3: 3.00
The reason for this is that 10/3 is integer division, see how many times 3 goes up completely in 10, which is 3 times.
If you want floating point division, divide 10 by 3 to get 3 and 1/3rd, then you need to make sure at least one of the numbers are floating point, hence 10.0/3 will do.
Upvotes: 9
Reputation: 117330
That's because your code is incorrect.
In more than 1 place!
(the brace and the missing '/3.0' part)
Upvotes: -1