String-Float conversion problem

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
7 posts • Page 1 of 1
weirdal
Posts:34
Joined: Thu Mar 12, 2015 11:37 am

String-Float conversion problem

Post by weirdal »

Hi,

we are experiencing the following problem:
We do have a Control library with a function that does - next to other stuff - a string to float conversion looking like this:

float value = stringArray[3];

This works in the following scenarios:
1) Windows 10 with script running in UI Manager
2) Windows 10 with script running in Ctrl Manager
3) Linux (CentOS 7) with script running in UI Manager

It does, however, not work on Linux with the script running in a Ctrl Manager. The decimal places are being cut off in this senario.

Any hints on how and why this would happen?

dbindernagel
Posts:161
Joined: Mon Feb 23, 2015 1:34 pm

Re: String-Float conversion problem

Post by dbindernagel »

That sounds like a problem with the decimal "point" character.

Code: Select all

string text1 = "123,45";
string text2 = "123.45";
float value1 = text1;
float value2 = text2;

DebugN("Value 1", value1);
DebugN("Value 2", value2);

Output:
WCCOAui7:["Value 1"][123]
WCCOAui7:["Value 2"][123.45]
Not sure why that only happens for a script running in a control manager on Linux. Maybe the string is different there and uses a "," instead of a ".".

Also see the help:
CONTROL\Introduction to CONTROL\Type conversions\Conversion from string values
Strings can easily be converted to int, unsigned or float. With float one must take care with the county setting for the output of numerical values. The decimal point character may change depending on the country.

leoknipp
Posts:2928
Joined: Tue Aug 24, 2010 7:28 pm

Re: String-Float conversion problem

Post by leoknipp »

Possibly you are using a multi language project and your CTRL manager runs with a different language than the UI manager.

Best Regards
Leopold Knipp
Senior Support Specialist

weirdal
Posts:34
Joined: Thu Mar 12, 2015 11:37 am

Re: String-Float conversion problem

Post by weirdal »

I was assuming that the CTRL manager may run under a different user than the UI manager.

How can I determine the decimal separator that is used for the active language?
And why would that only happen on Linux but not on Windows?

gschijndel
Posts:376
Joined: Tue Jan 15, 2019 3:12 pm

Re: String-Float conversion problem

Post by gschijndel »

If the formatting is important you should not use an explicit type conversion, but use a formatting function (like 'sprint*').

dbindernagel
Posts:161
Joined: Mon Feb 23, 2015 1:34 pm

Re: String-Float conversion problem

Post by dbindernagel »

Just to be clear: If you use the type conversion from sting to float it always expects the decimal point character to be a "." and not something else.
The type conversion does not take into account the setting of the operating system or the project language.

At least this is what I found testing it with the following settings:
WinCC OA V3.16 P015
Windows 10 with German Language; decimal point is ","
Project language: German, Austria (de_AT.iso88591) (only language in the project)

So either the type conversion behaves differently on Linux or the string you have in your "float value = stringArray[3];" is different while running on Linux in a control manager.

weirdal
Posts:34
Joined: Thu Mar 12, 2015 11:37 am

Re: String-Float conversion problem

Post by weirdal »

The original value is stored as part of a csv-string in DP_A.DPE_B and is supposed to be stored as a float value in newly created DP_X.DPE_Y.
So, the formatting is not important, but I would still like to keep those decimal places.

The value does always have a "." as decimal separator in the string, independent of the platform or manager.
Also, the control manager in linux is running under the same user as every other WinCC OA process.
Still, the conversion does not work.

I gave up on finding the root cause of this and am using this ugly piece of code to get around it:

public float convertTextToFloat(string s)
{
dyn_string split;
float result = 0;
float decimalNumbers = 0;

split = strsplit(s, ".");
if (dynlen(split) == 2)
decimalNumbers = (float)split[2] / pow(10, strlen(split[2]));

result = (float)split[1] + decimalNumbers;

return result;
}

However, any hints on the issue are still welcome.

7 posts • Page 1 of 1