0

Here's my code:

var myString = "1234.56";
var myFloat = float.Parse(myString);
Console.WriteLine(myFloat);

The output now is

123456

But why? It shouldn't be 123456, it should be 1234.56 Why is it done wrong? Is it because I work on a German machine? But that would be crazy. What if I gave my code to someone with an English machine? Would it work correctly then?

Because the fun thing is that the following would give me the correct float number (but it shouldn't):

var myString = "1234,56"; // the German way to write floating numbers
var myFloat = float.Parse(myString);
Console.WriteLine(myFloat);

The output here is

1234,56  // yes, for some reason with a comma!

I'm very confused now...

Andreas Utzinger
  • 392
  • 2
  • 12
  • https://stackoverflow.com/a/11203062/106159 – Matthew Watson Aug 29 '22 at 09:47
  • I guess, your [current culture](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture?view=net-6.0) is `de-DE` – Markus Meyer Aug 29 '22 at 09:47
  • Indeed it seems to be your culture settings. To set an "invariant" culture, have a look at: https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse – Francois Louw Aug 29 '22 at 09:49
  • Ok, very nice to know, thanks guys. Is there a way to set this globally somewhere in Visual Studio or would I have to install the English version of it? – Andreas Utzinger Aug 29 '22 at 09:52
  • 1
    @Andreas Visual Studio is the tool you use to author your code, it doesn't run your code. For how compiled .NET programs obtain their culture information, please see [this question](https://stackoverflow.com/questions/1745439/how-does-a-net-process-get-the-culture-information). _"Is it because I work on a German machine? But that would be crazy"_ - Why? It makes more sense than arbitrarily just always using the invariant culture, essentially forcing everyone to use the $ currency symbol, American date formats, number formats, etc. – ProgrammingLlama Aug 29 '22 at 10:00
  • The behaviour would be the same when compiled with an english VisualStudio, as this is runtime-behaviour and not related to the compiler. – Klaus Gütter Aug 29 '22 at 10:01
  • @AndreasUtzinger. I can also affirm the comments above... You should specify the culture info when you call the ```Parse``` or ```TryParse``` method. e.g. : ```Double.TryParse(propertyValue, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double doubleNumValue)```, where ```propertyValue``` is the string you would like to parse and doubleNumValue is the ```propertyValue``` converted to ```double``` if successful. – Francois Louw Aug 29 '22 at 10:10
  • But what if I send the whole project file to someone with an English machine and he or she compiles it again. Would the code still work correctly then? If so, then there must a culture setting somewhere in my project, right? But where? – Andreas Utzinger Aug 29 '22 at 10:22
  • @AndreasUtzinger The culture setting is for your PC. By default the code will parse data according to that culture setting. You can override this behaviour by specifying a culture when parsing. – Matthew Watson Aug 29 '22 at 10:27
  • 1
    If you're German then 1,234 is 1 with a decimal component of 234. If you send your program to someone in the UK and they want to input the same value, they would write 1.234. Moving away from numbers, think about dates. In France, a user would expect to be able to input 2 Avril 2022, and here in Japan I'd expect 2022年4月2日. Wouldn't it be bad if everyone had to write 2 April 2022? – ProgrammingLlama Aug 29 '22 at 10:57
  • But an English machine would output 1234.56 from the first three lines of my code (see start post). My system gives me 123456 though. These are completely different numbers. So I find it a bit "dangerous". And I'm talking about if an English machine compiles my code/project. Not about sending my compiled program to someone. – Andreas Utzinger Aug 29 '22 at 13:45
  • The compilation process doesn't bake in a culture. You could compile it on any machine in the world and you would still see the same result running the compiled program on your machine. On your machine 1234.56 has an oddly placed thousands separator. On mine that is a decimal point. The culture is determined when the program is run, not when it is compiled. Defaulting to the user's culture makes everything more predictable for the user. Specific cultures can be explicitly used by developers when needed. – ProgrammingLlama Aug 29 '22 at 16:19
  • Ok, then please tell me what the following code logs on your machine: **var myString = "1234.56"; var myFloat = float.Parse(myString); myFloat = myFloat + 100; Console.WriteLine(myFloat);** – Andreas Utzinger Aug 30 '22 at 13:56
  • My computer is set to English (UK) and it logs "1334.56". If I change my computer's "regional format" to French (France) and run the same executable (without recompiling) it throws an exception because it can't understand the `.`: "Unhandled exception. System.FormatException: Input string was not in a correct format.". Changing it to "1234,56" and rebuilding then outputs "1334,56". Of course if this were an input, I'd simply input the correct format for the culture. – ProgrammingLlama Aug 30 '22 at 14:05

0 Answers0