I have a problem while reading my DataBase. Until now I was sure that when I read some doubles from MySQL (they are stored as doubles not as strings / varchar), they are returned with a dot as decimal separator. (10.5). But now I notice that in fact, they are returned with a coma?
I lately created a new table (one month ago), but just now I noticed that its double values are returned with a comma ,
instead of the dot .
.
I checked and the tables are all in uff8_unicode_ci.
I don't understand because I was sure that ,
could not be used in MySQL, also checked that on that link.
Nota : This is maybe due to the fact that I use several computers for tests, some are in French, some in Russian?
Edit :
To edit the table I do the following :
string request = "UPDATE [vegasteel].consumables SET ID_MACHINE=" + this.idMachine + ",GROUPE="PLASMA",TYPE=\'\',DETAIL=\'14\',SPEED=10.5,TIME=5.5,COST=0.5 WHERE ID=" + this.id;
DataBase.Update(request);
Then to read it :
if (this.OpenConnection() == true)
{
IDataReader dataReader = ExecuteReader(query);
while (dataReader.Read())
{
Consumable consumable = new Consumable();
consumable.ID = Convert.ToInt64(dataReader["ID"].ToString());
consumable.IdMachine = Convert.ToInt64(dataReader["ID_MACHINE"].ToString());
consumable.Groupe = dataReader["GROUPE"].ToString();
consumable.Type = dataReader["TYPE"].ToString();
consumable.Detail = dataReader["DETAIL"].ToString();
if(consumable.Groupe=="PLASMA")
{
string toto = "";
}
consumable.Speed = Global.ConvertToDouble(dataReader["SPEED"].ToString());
consumable.Time = Global.ConvertToDouble(dataReader["TIME"].ToString());
consumable.Cost = Global.ConvertToDouble(dataReader["COST"].ToString());
list.Add(consumable);
}
this.CloseConnection();
}
the Global.ConvertToDouble
is a function I made to add a comma or a dot regarding the current culture. But the problem is when I read it, dataReader["COST"] equals 0,5
, and not 0.5
Edit 2 :
As asked, here is Global.ConvertToDouble function, but as I explained the problem is before that function, as comma is sent instead of dot to the function.
public static double ConvertToDouble(this string strToParse, char decimalSymbol = '.')
{
if (strToParse.Contains('E'))
{
strToParse = "0";
}
string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([\s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value;
if (tmp.Length > 0 && strToParse.Contains(tmp))
{
var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator;
tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator);
return double.Parse(tmp);
}
return double.NegativeInfinity;
}