Understand "," as "." when formatting


My Visual Studio is misunderstanding what I type (console):

using System;

namespace Uri_CSharp
    class URI
        static void Main(string[] args)
            double raio = double.Parse(Console.ReadLine()), area;
            area = Math.Pow(raio, 2) * 3.14159;
            Console.WriteLine("{0:F4}", area);

Entry: 2.0 (The way I would like to do the entry)

Exit: 1256,6360

Input: 2.0

Output: 12,5664 (Way I'd like to output, however, instead of.)

Are you sure you want to configure this?

asked by anonymous 17.07.2016 / 15:59

3 answers


I was able to: First I declare use of these two:

using System.Threading;

Then before starting to print to the console, I declare this:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
17.07.2016 / 16:36

First, Visual Studio is just an IDE , it helps develop, it does not run, it does not have who understand nothing.

Second, the code executes what was ordered, the computer "understands" what the programmer ordered. Who has to understand what he is doing is the programmer.

There are several ways to resolve this. The question does not make it very clear what you need to do so I'll answer what I think you want.

You need to consider the culture to be used for printing it.

It's also better to use a TryParse() since you are not sure that a value will be typed can be converted.

I took advantage of and updated the code.

using static System.Console;
using static System.Math;
using System.Globalization;

namespace Uri_CSharp {
    public class URI {
        public static void Main(string[] args) {
            var texto = ReadLine();
            double raio;
            if (double.TryParse(texto, out raio)) { 
                double area = Pow(raio, 2) * 3.14159;
                WriteLine(area.ToString("F4", new CultureInfo("pt-BR")));

See running on dotNetFiddle and on CodingGround .

You can define the default culture for a thread , so runtime considers it instead of what is configured on the computer:

Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
17.07.2016 / 16:22

You can use Replace in the example data entry.

entry     1,200.Replace (",", "."); // Take the comma and put the point. Exit     1,200

I hope I have helped.

18.07.2016 / 18:29