[solved] Can't succeed to convert string to float

7 years 11 months ago - 7 years 11 months ago #670 by Xavier
I've already spent several hours on the subject and can't find my way to convert string value to float. I guess something is broken in the automation scripting.
Even MSDN sample code doesn't work and I don't know why.
Could you test this and tell me if you get the same results (it doesn't depends on domains, nodes and modules ) .
// ============================================================================
//  Finding a way to convert strings extracted from sqlite into float.
//  This reference code doesn't work but it comes from :
//  https://msdn.microsoft.com/fr-fr/library/3s27fasw%28v=vs.110%29.aspx
// ============================================================================


public void Setup()
{
  string value;
  double number;
  System.Globalization.NumberStyles style;
  System.Globalization.CultureInfo culture;
  
  // Parse currency value using en-GB culture.
  // -----------------------------------------
  value = "£1,097.63";
  style = System.Globalization.NumberStyles.Number | System.Globalization.NumberStyles.AllowCurrencySymbol;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
  
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '£1,097.63' to 1097.63.
  // but I get:
  //	   Unable to convert £1,097.63

  // Parse decimal value using fr-FR culture.
  // -----------------------------------------
  value = "1345,978";
  style = System.Globalization.NumberStyles.AllowDecimalPoint;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '1345,978' to 1345.978.
  // but I get:
  //	   Unable to convert 1345,978


  // Parse decimal value with thousands separator using es-ES culture.
  // -----------------------------------------
  value = "1.345,978";
  style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '1.345,978' to 1345.978.
  // but I get:
  //	   Unable to convert 1.345,978

  // Parse mismatching decimal value with same es-ES culture.
  // -----------------------------------------
  value = "1 345,978";
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Does display: 
  //       Unable to convert '1 345,978'.
}

public void Run()
{
}

Any clue?

Please Log in or Create an account to join the conversation.

7 years 11 months ago #681 by EasyIoT

Xavier wrote: I've already spent several hours on the subject and can't find my way to convert string value to float. I guess something is broken in the automation scripting.
Even MSDN sample code doesn't work and I don't know why.
Could you test this and tell me if you get the same results (it doesn't depends on domains, nodes and modules ) .

// ============================================================================
//  Finding a way to convert strings extracted from sqlite into float.
//  This reference code doesn't work but it comes from :
//  https://msdn.microsoft.com/fr-fr/library/3s27fasw%28v=vs.110%29.aspx
// ============================================================================


public void Setup()
{
  string value;
  double number;
  System.Globalization.NumberStyles style;
  System.Globalization.CultureInfo culture;
  
  // Parse currency value using en-GB culture.
  // -----------------------------------------
  value = "£1,097.63";
  style = System.Globalization.NumberStyles.Number | System.Globalization.NumberStyles.AllowCurrencySymbol;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
  
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '£1,097.63' to 1097.63.
  // but I get:
  //	   Unable to convert £1,097.63

  // Parse decimal value using fr-FR culture.
  // -----------------------------------------
  value = "1345,978";
  style = System.Globalization.NumberStyles.AllowDecimalPoint;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '1345,978' to 1345.978.
  // but I get:
  //	   Unable to convert 1345,978


  // Parse decimal value with thousands separator using es-ES culture.
  // -----------------------------------------
  value = "1.345,978";
  style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands;
  culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Should display: 
  //       Converted '1.345,978' to 1345.978.
  // but I get:
  //	   Unable to convert 1.345,978

  // Parse mismatching decimal value with same es-ES culture.
  // -----------------------------------------
  value = "1 345,978";
  if (Double.TryParse(value, style, culture, out number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);
  else
    Console.WriteLine("Unable to convert '{0}'.", value);
  // Does display: 
  //       Unable to convert '1 345,978'.
}

public void Run()
{
}

Any clue?


You can try p.DecimalValue property to read numeric value.

Please Log in or Create an account to join the conversation.

7 years 11 months ago - 7 years 11 months ago #684 by Xavier

EasyIoT wrote: You can try p.DecimalValue property to read numeric value.


Thank you for the answer, I already tried that but I get a compilation error:
Errors:
Error CS1061 in line 50 column 132, Type `GenericDriver.DriverPropertyChangedData' does not contain a definition for `DecimalValue' and no extension method `DecimalValue' of type `GenericDriver.DriverPropertyChangedData' could be found. Are you missing an assembly reference?
Error in line -11 column 0, /home/pi/Code/easyiot/GenericDriver.dll (Location of the symbol related to previous error)
Program disabled, fix errors first.
Here is my test code:
const string testNode = "N2S0";
enum HState : byte {None,High,Low};
static HState status = HState.None;
static Int32 count =  0;
public static DateTime lastON = new DateTime(2015, 3, 1, 0, 0, 0);

public void Setup()
{  
  try
  {
    EventHelper.ModuleChangedHandler ((o, m, p) =>
    {
      Console.WriteLine("\t DBG: prog {0}.dll {1}.{2} {3} = {4}", Program.ProgramId.ToString(), m.Domain, m.Address, p.Property, p.DecimalValue);

      if (m.Domain == "MySensors" && m.Address == testNode && p.Property == "Sensor.Humidity")
      {
        Console.WriteLine("\t DBG: Count = {0}, Status = {1} LastOn = {2}",count,status,lastON);
        count ++;
      }
      return true;
    });
  }
  catch(Exception e)
  {
    Console.WriteLine(e.StackTrace);
  }
}

Please Log in or Create an account to join the conversation.

7 years 11 months ago #689 by EasyIoT

Xavier wrote:

EasyIoT wrote: You can try p.DecimalValue property to read numeric value.


Thank you for the answer, I already tried that but I get a compilation error:
Errors:
Error CS1061 in line 50 column 132, Type `GenericDriver.DriverPropertyChangedData' does not contain a definition for `DecimalValue' and no extension method `DecimalValue' of type `GenericDriver.DriverPropertyChangedData' could be found. Are you missing an assembly reference?
Error in line -11 column 0, /home/pi/Code/easyiot/GenericDriver.dll (Location of the symbol related to previous error)
Program disabled, fix errors first.
Here is my test code:
const string testNode = "N2S0";
enum HState : byte {None,High,Low};
static HState status = HState.None;
static Int32 count =  0;
public static DateTime lastON = new DateTime(2015, 3, 1, 0, 0, 0);

public void Setup()
{  
  try
  {
    EventHelper.ModuleChangedHandler ((o, m, p) =>
    {
      Console.WriteLine("\t DBG: prog {0}.dll {1}.{2} {3} = {4}", Program.ProgramId.ToString(), m.Domain, m.Address, p.Property, p.DecimalValue);

      if (m.Domain == "MySensors" && m.Address == testNode && p.Property == "Sensor.Humidity")
      {
        Console.WriteLine("\t DBG: Count = {0}, Status = {1} LastOn = {2}",count,status,lastON);
        count ++;
      }
      return true;
    });
  }
  catch(Exception e)
  {
    Console.WriteLine(e.StackTrace);
  }
}


Try this:
public void Setup()
{
	EventHelper.ModuleChangedHandler((o, m, p) =>
    {
    	Console.WriteLine(m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);
      
       ModuleParameter p1 = ModuleHelper.GetProperty(m.Domain, m.Address, p.Property);
      
      float f = (float)p1.DecimalValue;
      
      Console.WriteLine("Float value: " + f);

        return true;
    });
}

Please Log in or Create an account to join the conversation.

7 years 11 months ago #693 by Xavier
I've created a new automation program with your exact code and I always get on stdout :
Float value: 0
2015-03-03T21:01:48.2237250+01:00       INFO    EasyIoTWebinterface     EasyIoTConfig   AutomationProgram       22      /Api/EasyIoT/Config/AutomationProgram/22/Update//
2015-03-03T21:01:52.3347710+01:00       DEBUG   MySensors                               Read: 2-2-0 s=0,c=1,t=1,pt=7,l=5:57.4
2015-03-03T21:01:52.3418990+01:00       INFO    MySensors       N2S0    Sensor.Humidity 57.4    -
2015-03-03T21:01:52.3707360+01:00       DEBUG   MySensors                               Read: 2-2-0 s=1,c=1,t=0,pt=7,l=5:19.9
2015-03-03T21:01:52.3770710+01:00       INFO    MySensors       N2S1    Sensor.Temperature      19.9    -
2015-03-03T21:01:52.3997390+01:00       DEBUG   MySensors                               Read: 2-2-0 s=2,c=1,t=1,pt=7,l=5:56.2
2015-03-03T21:01:52.4075340+01:00       INFO    MySensors       N2S2    Sensor.Humidity 56.2    -
2015-03-03T21:01:52.4267210+01:00       DEBUG   MySensors                               Read: 2-2-0 s=3,c=1,t=0,pt=7,l=5:19.5
2015-03-03T21:01:52.4330690+01:00       INFO    MySensors       N2S3    Sensor.Temperature      19.5    -
2015-03-03T21:01:56.0267390+01:00       DEBUG   MySensors                               Read: 1-1-0 s=0,c=1,t=1,pt=7,l=5:52.2
2015-03-03T21:01:56.0330270+01:00       INFO    MySensors       N1S0    Sensor.Humidity 52.2    -
2015-03-03T21:01:56.0627280+01:00       DEBUG   MySensors                               Read: 1-1-0 s=1,c=1,t=0,pt=7,l=5:21.0
2015-03-03T21:01:56.0697330+01:00       INFO    MySensors       N1S1    Sensor.Temperature      21.0    -
2015-03-03T21:01:56.0907240+01:00       DEBUG   MySensors                               Read: 1-1-0 s=2,c=1,t=1,pt=7,l=5:51.1
2015-03-03T21:01:56.0971590+01:00       INFO    MySensors       N1S2    Sensor.Humidity 51.1    -
2015-03-03T21:01:56.1217320+01:00       DEBUG   MySensors                               Read: 1-1-0 s=3,c=1,t=0,pt=7,l=5:21.3
2015-03-03T21:01:56.1283900+01:00       INFO    MySensors       N1S3    Sensor.Temperature      21.3    -
2015-03-03T21:02:05.0876410+01:00       DEBUG   MySensors                               Read: 2-2-0 s=0,c=1,t=1,pt=7,l=5:57.4
2015-03-03T21:02:05.0955670+01:00       INFO    MySensors       N2S0    Sensor.Humidity 57.4    -
N2S0 in program id 22 property Sensor.Humidity value 57.4
2015-03-03T21:02:05.1218330+01:00       DEBUG   MySensors                               Read: 2-2-0 s=1,c=1,t=0,pt=7,l=5:19.9
2015-03-03T21:02:05.1292140+01:00       INFO    MySensors       N2S1    Sensor.Temperature      19.9    -
N2S1 in program id 22 property Sensor.Temperature value 19.9
2015-03-03T21:02:05.1638100+01:00       DEBUG   MySensors                               Read: 2-2-0 s=2,c=1,t=1,pt=7,l=5:56.2
2015-03-03T21:02:05.1727420+01:00       INFO    MySensors       N2S2    Sensor.Humidity 56.2    -
2015-03-03T21:02:05.2027320+01:00       DEBUG   MySensors                               Read: 2-2-0 s=3,c=1,t=0,pt=7,l=5:19.5
2015-03-03T21:02:05.2090600+01:00       INFO    MySensors       N2S3    Sensor.Temperature      19.5    -
Float value: 0
N2S2 in program id 22 property Sensor.Humidity value 56.2
Float value: 0
N2S3 in program id 22 property Sensor.Temperature value 19.5
Float value: 0
Float value: 0
2015-03-03T21:02:08.9066520+01:00       DEBUG   MySensors                               Read: 1-1-0 s=0,c=1,t=1,pt=7,l=5:52.2
2015-03-03T21:02:08.9128210+01:00       INFO    MySensors       N1S0    Sensor.Humidity 52.2    -
N1S0 in program id 22 property Sensor.Humidity value 52.2
Float value: 0

Please Log in or Create an account to join the conversation.

7 years 11 months ago #694 by EasyIoT

Xavier wrote: I've created a new automation program with your exact code and I always get on stdout :

Float value: 0
2015-03-03T21:01:48.2237250+01:00       INFO    EasyIoTWebinterface     EasyIoTConfig   AutomationProgram       22      /Api/EasyIoT/Config/AutomationProgram/22/Update//
2015-03-03T21:01:52.3347710+01:00       DEBUG   MySensors                               Read: 2-2-0 s=0,c=1,t=1,pt=7,l=5:57.4
2015-03-03T21:01:52.3418990+01:00       INFO    MySensors       N2S0    Sensor.Humidity 57.4    -
2015-03-03T21:01:52.3707360+01:00       DEBUG   MySensors                               Read: 2-2-0 s=1,c=1,t=0,pt=7,l=5:19.9
2015-03-03T21:01:52.3770710+01:00       INFO    MySensors       N2S1    Sensor.Temperature      19.9    -
2015-03-03T21:01:52.3997390+01:00       DEBUG   MySensors                               Read: 2-2-0 s=2,c=1,t=1,pt=7,l=5:56.2
2015-03-03T21:01:52.4075340+01:00       INFO    MySensors       N2S2    Sensor.Humidity 56.2    -
2015-03-03T21:01:52.4267210+01:00       DEBUG   MySensors                               Read: 2-2-0 s=3,c=1,t=0,pt=7,l=5:19.5
2015-03-03T21:01:52.4330690+01:00       INFO    MySensors       N2S3    Sensor.Temperature      19.5    -
2015-03-03T21:01:56.0267390+01:00       DEBUG   MySensors                               Read: 1-1-0 s=0,c=1,t=1,pt=7,l=5:52.2
2015-03-03T21:01:56.0330270+01:00       INFO    MySensors       N1S0    Sensor.Humidity 52.2    -
2015-03-03T21:01:56.0627280+01:00       DEBUG   MySensors                               Read: 1-1-0 s=1,c=1,t=0,pt=7,l=5:21.0
2015-03-03T21:01:56.0697330+01:00       INFO    MySensors       N1S1    Sensor.Temperature      21.0    -
2015-03-03T21:01:56.0907240+01:00       DEBUG   MySensors                               Read: 1-1-0 s=2,c=1,t=1,pt=7,l=5:51.1
2015-03-03T21:01:56.0971590+01:00       INFO    MySensors       N1S2    Sensor.Humidity 51.1    -
2015-03-03T21:01:56.1217320+01:00       DEBUG   MySensors                               Read: 1-1-0 s=3,c=1,t=0,pt=7,l=5:21.3
2015-03-03T21:01:56.1283900+01:00       INFO    MySensors       N1S3    Sensor.Temperature      21.3    -
2015-03-03T21:02:05.0876410+01:00       DEBUG   MySensors                               Read: 2-2-0 s=0,c=1,t=1,pt=7,l=5:57.4
2015-03-03T21:02:05.0955670+01:00       INFO    MySensors       N2S0    Sensor.Humidity 57.4    -
N2S0 in program id 22 property Sensor.Humidity value 57.4
2015-03-03T21:02:05.1218330+01:00       DEBUG   MySensors                               Read: 2-2-0 s=1,c=1,t=0,pt=7,l=5:19.9
2015-03-03T21:02:05.1292140+01:00       INFO    MySensors       N2S1    Sensor.Temperature      19.9    -
N2S1 in program id 22 property Sensor.Temperature value 19.9
2015-03-03T21:02:05.1638100+01:00       DEBUG   MySensors                               Read: 2-2-0 s=2,c=1,t=1,pt=7,l=5:56.2
2015-03-03T21:02:05.1727420+01:00       INFO    MySensors       N2S2    Sensor.Humidity 56.2    -
2015-03-03T21:02:05.2027320+01:00       DEBUG   MySensors                               Read: 2-2-0 s=3,c=1,t=0,pt=7,l=5:19.5
2015-03-03T21:02:05.2090600+01:00       INFO    MySensors       N2S3    Sensor.Temperature      19.5    -
Float value: 0
N2S2 in program id 22 property Sensor.Humidity value 56.2
Float value: 0
N2S3 in program id 22 property Sensor.Temperature value 19.5
Float value: 0
Float value: 0
2015-03-03T21:02:08.9066520+01:00       DEBUG   MySensors                               Read: 1-1-0 s=0,c=1,t=1,pt=7,l=5:52.2
2015-03-03T21:02:08.9128210+01:00       INFO    MySensors       N1S0    Sensor.Humidity 52.2    -
N1S0 in program id 22 property Sensor.Humidity value 52.2
Float value: 0


Hmm, put this in Setup first line:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

Please Log in or Create an account to join the conversation.

Time to create page: 0.266 seconds

Forum latest

  • No posts to display.