Bug 22689 - Parser should not use users locale for floating points in code
Summary: Parser should not use users locale for floating points in code
Status: RESOLVED NORESPONSE
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.2.x
Hardware: Other Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-09-05 08:33 UTC by Soenke Pries
Modified: 2017-07-28 10:04 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Soenke Pries 2014-09-05 08:33:55 UTC
When compiling sourcecode in MonoDevelop (or MCS) in a german (or, better yet, non-american) environment, the parser throws exceptions like "Parser Error: The floating point value is not in the correct format".

The floating points in source code are always in american format, so don't use the users locale for parsing.

Steps to reproduce:
1. install a new (for example) Raspberry Pi.
2. Change the system locale to german using Raspi-Config.
2. Install Mono and, if you want, MonoDevelop.
3. Open the editor, assign a literal floating point value in code.
4. Try to compile.

Cpmpiling the same code without changing the locale works.
Comment 1 Marek Safar 2014-09-08 09:13:24 UTC
I cannot reproduce the issue. I don't have Raspberry Pi but it works for me on mac

using System;

class Program
{
    static void Main ()
    {
        const double f = 0.2;
    }
}
[mono] ~/git/mono/mcs/mcs$ locale
LANG="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_CTYPE="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_ALL=
[mono] ~/git/mono/mcs/mcs$ mcs test-mcs2.cs
Comment 2 Soenke Pries 2014-09-10 11:00:39 UTC
This time, my MonoDevelop crsashed even when starting up with 

System.FormatException: Input string was not in the correct format
  at System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at System.Single.Parse (System.String s, IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at System.Convert.ToSingle (System.String value, IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at System.String.System.IConvertible.ToSingle (IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename unknown>:0 
  at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at Stetic.PropertyDescriptor.StringToValue (System.String value) [0x00000] in <filename unknown>:0 
  at Stetic.PropertyDescriptor.Load (System.Xml.XmlElement elem) [0x00000] in <filename unknown>:0 
  at Stetic.TypedPropertyDescriptor..ctor (System.Xml.XmlElement elem, Stetic.ItemGroup group, Stetic.TypedClassDescriptor klass) [0x00000] in <filename unknown>:0 
  at Stetic.TypedClassDescriptor.CreateItemDescriptor (System.Xml.XmlElement elem, Stetic.ItemGroup group) [0x00000] in <filename unknown>:0 
  at Stetic.ItemGroup..ctor (System.Xml.XmlElement elem, Stetic.ClassDescriptor klass) [0x00000] in <filename unknown>:0 
  at Stetic.ClassDescriptor.Load (System.Xml.XmlElement elem) [0x00000] in <filename unknown>:0 
  at Stetic.TypedClassDescriptor..ctor (System.Reflection.Assembly assembly, System.Xml.XmlElement elem) [0x00000] in <filename unknown>:0 
  at Stetic.AssemblyWidgetLibrary.LoadClassDescriptor (System.Xml.XmlElement element) [0x00000] in <filename unknown>:0 
  at Stetic.WidgetLibrary.Load (System.Xml.XmlDocument objects) [0x00000] in <filename unknown>:0 
  at Stetic.AssemblyWidgetLibrary.Load () [0x00000] in <filename unknown>:0 
  at Stetic.Registry.RegisterWidgetLibrary (Stetic.WidgetLibrary library) [0x00000] in <filename unknown>:0
Comment 3 Soenke Pries 2014-09-10 11:01:54 UTC
But when started, if I use 

var doub = 2.5F;

or 

double doub = 2.5;

always give the exception

Test1 (Debug|x86) wird erzeugt
Performing main compilation...
/usr/bin/mcs /noconfig "/r:/usr/lib/mono/4.0/mscorlib.dll" -nostdlib "/out:/home/bananapi/Work/Test1/Test1/bin/Debug/Test1.exe" "/r:/usr/lib/mono/4.0/System.dll" "/r:/usr/lib/mono/4.0/System.Core.dll" /nologo /warn:4 /debug:full /optimize- /codepage:utf8 /platform:x86 "/define:DEBUG"  /t:exe "/home/bananapi/Work/Test1/Test1/Main.cs" "/home/bananapi/Work/Test1/Test1/AssemblyInfo.cs" 
Compilation failed: 1 error(s), 0 warnings

/home/bananapi/Work/Test1/Test1/Main.cs(10,20): error CS0589: Internal compiler error during parsingSystem.FormatException: Input string was not in the correct format
  at System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Tokenizer.adjust_real (TypeCode t, Location loc) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Tokenizer.is_number (Int32 c, Boolean dotLead) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Tokenizer.xtoken () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Tokenizer.token () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.CSharpParser.yyparse (yyInput yyLex) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.CSharpParser.parse () [0x00000] in <filename unknown>:0 


Erzeugung abgeschlossen -- 1 Fehler, 0 Warnungen
Comment 4 Marek Safar 2014-09-10 13:33:41 UTC
I'm really unsure what is going on here, IFormatProvider is invariant formatter. Could you try to reproduce it in C# console app where you do

double.Parse ("2.5", NumberStyles.Float, NumberFormatInfo.InvariantInfo);
Comment 5 X. Muller 2015-03-05 06:44:37 UTC
I'm facing the same issue on a French Raspberry Pi B Raspbian runing latest mono-complete installed through apt-get, that is v3.2.8.
Every method I could use to convert string to float fails. Even a simple const float declaration fails with exeption: "incorrect input string format"
// System.Globalization.NumberFormatInfo nfi = System.Globalization.NumberFormatInfo.InvariantInfo;
// fValue = Convert.ToSingle(p.Value,nfi);
// fValue = float.Parse(p.Value.Replace('.',','));
// fValue = Single.Parse(p.Value,new System.Globalization.CultureInfo("en-US"));
// fValue = Single.Parse(testFloat,new System.Globalization.CultureInfo("en-US"));
and the following instruction evals to false:
float.TryParse("64.6", System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture,out fValue)

 The workaround I had to use were:

const float val = 566f/10f;
float StrToFloat(string sVal)
{
  string[] sSingle;
  float Val;
 
  sSingle = sVal.Split(new char[1] {'.'});
  Val = Convert.ToInt32(sSingle[0]) * 10 + Convert.ToInt32(sSingle[1][0]);

  return Val / 10;
} 

@Marek Safar
Just FYI, your instruction is accepted by the compiler but when the code is run I get an exception (the C# console App here is provided by EasyIoT.exe v07b2:
  at System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at EasyIoT.Automation.ScriptingInstance.Setup () [0x00000] in <filename unknown>:0

$ mono -V
Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-4+rpi1)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       normal
        Notifications: epoll
        Architecture:  armel,vfp+hard
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

$ uname -a
Linux raspberrypi 3.18.7+ #756 PREEMPT Wed Feb 18 16:08:51 GMT 2015 armv6l GNU/Linux

$ locale
LANG=fr_FR.utf8
LANGUAGE=
LC_CTYPE="fr_FR.utf8"
LC_NUMERIC="fr_FR.utf8"
LC_TIME="fr_FR.utf8"
LC_COLLATE="fr_FR.utf8"
LC_MONETARY="fr_FR.utf8"
LC_MESSAGES="fr_FR.utf8"
LC_PAPER="fr_FR.utf8"
LC_NAME="fr_FR.utf8"
LC_ADDRESS="fr_FR.utf8"
LC_TELEPHONE="fr_FR.utf8"
LC_MEASUREMENT="fr_FR.utf8"
LC_IDENTIFICATION="fr_FR.utf8"
LC_ALL=

$ locale -a
C
C.UTF-8
en_GB.utf8
fr_FR.utf8
POSIX

My minimalist sample code (float_issue.cs):
public class Test {
  const float val = 56.6f;
  public static void Main (string[] args) {
	System.Console.WriteLine ("Value={0:F2}",val);
  }
}

When I compile in mono, I get:
 $ mcs float_issue.cs
float_issue.cs(2,25): error CS0589: Internal compiler error during parsingSystem.FormatException: Input string was not in the correct format
  at System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at System.Single.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Tokenizer.adjust_real (TypeCode t, Location loc) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Tokenizer.is_number (Int32 c, Boolean dotLead) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Tokenizer.xtoken () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Tokenizer.token () [0x00000] in <filename unknown>:0
  at Mono.CSharp.CSharpParser.yyparse (yyInput yyLex) [0x00000] in <filename unknown>:0
  at Mono.CSharp.CSharpParser.parse () [0x00000] in <filename unknown>:0
Compilation failed: 1 error(s), 0 warnings

If I unset $LANG or set to en_GB, it compiles OK.
$ export LANG="en_GB.utf8"
$ locale
LANG=en_GB.utf8
LANGUAGE=
LC_CTYPE="en_GB.utf8"
LC_NUMERIC="en_GB.utf8"
LC_TIME="en_GB.utf8"
LC_COLLATE="en_GB.utf8"
LC_MONETARY="en_GB.utf8"
LC_MESSAGES="en_GB.utf8"
LC_PAPER="en_GB.utf8"
LC_NAME="en_GB.utf8"
LC_ADDRESS="en_GB.utf8"
LC_TELEPHONE="en_GB.utf8"
LC_MEASUREMENT="en_GB.utf8"
LC_IDENTIFICATION="en_GB.utf8"
LC_ALL=

And running the .exe give me the attended result:
$ mono float_issue.exe
Value=56.60

Back to French locale, the float_issue.exe still works.
--
I hope those informations will help you to trouble shot the issue.
If you can't reproduce this behaviour neither, feel free to ask for more tests.
Comment 6 X. Muller 2015-03-05 06:56:50 UTC
It seems to be related to hard float specific code too (cf. bug 7938) it may be the reason why you cant reproduce on MacOSX.
Comment 7 Marek Safar 2016-09-16 09:03:07 UTC
Can you still reproduce it with Mono 4.6 or 4.8?
Comment 8 Marek Safar 2017-07-28 10:04:38 UTC
Closing. Please reopen if you still experiencing the issue with 5.x versions of Mono