Understand "," as "." when formatting

Asked

Viewed 134 times

3

My Visual Studio is getting a little wrong 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);
        }
    }
}

Input: 2.0 (way I would like to make the input)

Output: 1256,6360

Input: 2.0

Output: 12.5664 (way I would like to output though with . instead of ,)

You have where to set it up?

3 answers

3

First, Visual Studio is just an IDE, he helps develop, he not executed, he doesn’t have to understand anything.

Second, the code performs what it was ordered to do, the computer "understands" what the programmer ordered it to do. Who has to understand what is doing is the programmer.

There are several ways to solve this. The question does not make very clear what you need to do so I will answer what I think you want.

It is necessary to consider the culture to be used in his printing.

Also it is better to use one TryParse() since it is not certain that a value will be entered that can be converted.

I took advantage and gave a modernized in 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) {
            if (double.TryParse(ReadLine(), out var raio)) {
                double area = Pow(raio, 2) * 3.14159;
                WriteLine(area.ToString("F4", new CultureInfo("pt-BR")));
            }
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

It is possible to define the standard culture for a thread, thus the Runtime consider it in place of what is configured on the computer:

Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
  • My question was because if I compile the same code on the site http://repl.it passes exactly as I want, already in the visual studio installed on my PC is different, I imagined that there is an option in the properties of the program

  • It exists on the computer. If you don’t tell us what the culture is, the code will run the computer culture. You have to choose to use a computer culture or a specific one. The default is to let the computer "decide".

  • I would have to change the language of windows?

  • No, just the regional setting.

  • Can you tell me where that is?

  • Aliaz, would have a way to decide inside the code?

  • That’s what I said.

  • But in its form, I define whenever I will convert, would have some way to already leave defined?

  • Leave defined where?

  • At the beginning of the code, for example, already leave the culture the way I defined forever that printing already leave the way the culture leaves

  • http://answall.com/questions/33564/trocando-a-culture-do-sistema I saw this post, however, I could not declare this thread, it is here as if it did not exist

  • I put in the reply.

  • No no, it just missed there, the solution I found was: Thread.CurrentThread.Currentculture = new Cultureinfo("en-US"); It is neither Currentuiculture nor en-BR

  • What you will put, depends on what you want, the question does not make clear what you want.

Show 9 more comments

0


I got it this way: First I declared use of these two:

using.System.Globalization;
using System.Threading;

Then before I started printing on the console, I declared it:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

-1

You can use Replace, in the data entry, example.

entree 1,200.Replace(",","."); //Remove the comma and place the point. Exit 1,200

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.