jamesmontemagno / SettingsPlugin

Read and Write Settings Plugin for Xamarin and Windows
MIT License
324 stars 80 forks source link

Incorrect handling of culture dependend values like double and float #126

Closed Stefan-deHoDev closed 6 years ago

Stefan-deHoDev commented 6 years ago

Double and float values are not saved/loaded correctly on system with a language where the decimal symbol is "," instead of "."

Currently I tested this in a WPF app and on UWP, WPF fails, while UWP shows no such problem. I'll run this same test on android but don't have the means to test it on iOS. Will update this post or comments with further findings, when I have the time.

Bug Information

Version Number of Plugin: 3.1.1 Device Tested On: Windows 10 Simulator Tested On: Version of VS: Visual Studio 2017 Version of Xamarin: - Versions of other things you are using: -

Steps to reproduce the Behavior

Create a new WPF project

Add Xam.Plugins.Settings nuget package

Add this code after "InitializeComponent();"

TextBlock output = new TextBlock();
this.Content = output;

var settings = CrossSettings.Current;
double d = 4.3d;
float f = 1.4f;
decimal dec = new decimal(1.3d);
DateTime time = new DateTime(2017, 3, 3, 10, 10, 10);

settings.AddOrUpdateValue("double", d);
settings.AddOrUpdateValue("float", f);
settings.AddOrUpdateValue("decimal", dec);
settings.AddOrUpdateValue("datetime", time);

double resdouble = settings.GetValueOrDefault("double", 0d);
float resfloat = settings.GetValueOrDefault("float", 0f);
decimal resdecimal = settings.GetValueOrDefault("decimal", new decimal(0));
DateTime resdatetime = settings.GetValueOrDefault("datetime", DateTime.MinValue);

output.Text = "";
output.Text += $"Result double: {resdouble}, Equal: {resdouble == d}" + Environment.NewLine;
output.Text += $"Result float: {resfloat}, Equal: {resfloat == f}" + Environment.NewLine;
output.Text += $"Result decimal: {resdecimal}, Equal: {resdecimal == dec}" + Environment.NewLine;
output.Text += $"Result datetime: {resdatetime}, Equal: {resdatetime == time}" + Environment.NewLine;
  1. Open System Settings "Region & Language", click on "Additional date, time, & regional settings", click on "Change date, time, or number formats", select a format that uses "," instead of "." as decimal symbol or set this manually by going to "Additional settings..."

  2. Run wpf project

Expected Behavior

Each line should end in "True". Double result should be 4.3, float should be 1.4.

Values should be saved in a culture independed way, to account for the user switching system language/number format.

Actual Behavior

Double and float both fail to be correctly handled. Both ignore the decimal symbol resulting in values of 43 instead of 4.3 and 14 instead of 1.4. Decimal variable is correct though.

Correct result, when systems language is set to a language having "." as decimal symbol.

Code snippet

see above

Stefan-deHoDev commented 6 years ago

Updated with info: UWP seems to work, even switching between languages, the previous values are read correctly