C # argument error

I am making an example from the book: The Complete C # 3.0 Reference by Herbert Schildt. It's about writing to Console.WriteLine with arguments. Here's an 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 there is a bug in the book or is this my code? I would appreciate your help. Thanks to

One of the simplest ways to specify a format is to describe the template that WriteLine () will use. To do this, show an example of the format you want using # to mark the position digit. You can also specify decimal point and commas. For example, here's the best way to display 10 divided by 3:
Console.WriteLine ("Here 10/3: {0: #. ##}", 10.0 / 3.0);
  The conclusion from this statement is shown here: Here 10/3: 3.33

Btw this my code looks like this:

   static void Main()
    { 
   Console.WriteLine("Here is 10/3: {0:#.##)", 10);
    }

      

+1


a source to share


9 replies


Invalid trailing parenthesis used for format parameter.

Note the ending parenthesis) after the #. ##, it should be in place of} (curly braces).

Also note that you left the section, and if you just change your code to this (adjust the brace):

static void Main()
{ 
    Console.WriteLine("Here is 10/3: {0:#.##}", 10/3);
}

      



Then you will also have a different question, as the result of this would be:

Here is 10/3: 3.00

      

The reason for this is that 10/3 is an integer division, see how many times 3 is fully increased by 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 is floating point, so 10.0 / 3 will do.

+9


a source


Your code is wrong. You have used a parenthesis instead of a curly brace in a string literal. Try the following:



static void Main()
    { 
       Console.WriteLine("Here is 10/3: {0:#.##}", 10);
    }

      

+3


a source


Change )

to }

- it's just a typo.

+2


a source


Your format string is incorrect. You have {

paired with)

+2


a source


It should be:

Console.WriteLine("Here is 10/3: {0:#.##}", 10);

      

When you use a format, you must put it in {and}

+1


a source


Corrected code below:

static void Main()    
{    
Console.WriteLine("Here is 10/3: {0:#.##}", 10.0/3);    
}

      

+1


a source


at the end of the line you have a) instead of a}

+1


a source


Let me also send an identical answer to everyone else for no reason!

Replace ##) with ##} in your code.

0


a source


This is because your code is wrong.

In more than one place!

(parenthesis and missing part "/3.0")

-1


a source







All Articles