EConvertError

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

EConvertError

Postby Lena » Thu Mar 19, 2015 12:21 am

In StrToFloat I got EConvertError.

Is it correct fix like this:
Code: Select all
String JsonMemberArraySensValue = JSONPair->JsonValue->ToString();
wchar_t FS = FormatSettings.DateSeparator;
                  try
                   {
                    FormatSettings.DecimalSeparator = '.';
                    double d = 0;
                    JsonMemberArraySensValue = StringReplace(JsonMemberArraySensValue, ",", ".",TReplaceFlags()<<rfReplaceAll<<rfIgnoreCase);
                    bool chek = TryStrToFloat(JsonMemberArraySensValue,d);
                    if(chek)
                     temperature = d;
                    }
                    __finally
                        {
                         FormatSettings.DateSeparator = FS;
                        }

No error now.
Lena
BCBJ Master
BCBJ Master
 
Posts: 567
Joined: Sun Feb 06, 2011 1:28 pm

Re: EConvertError

Postby rlebeau » Thu Mar 19, 2015 1:40 pm

Lena wrote:In StrToFloat I got EConvertError.


You are not using StrToFloat(), you are using TryStrToFloat() instead, which does not raise an exception on failure.

Lena wrote:Is it correct fix like this


No. The global FormatSettings structure is not thread-safe. You should instead create a local TFormatSettings variable and customize it as needed, and then pass it to (Try)StrToFloat() as an input parameter (even without threads involved, I would still suggest doing things this way when performing customized parsing):

Code: Select all
TFormatSettings Fmt = TFormatSettings::Create(); // get default settings first...
Fmt.DecimalSeparator = ','; // now customize...
// no need for StringReplace()...
double d = 0;
if (TryStrToFloat(JSONPair->JsonValue->ToString(), d, Fmt))
    temperature = d;
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1528
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: EConvertError

Postby Lena » Fri Mar 20, 2015 1:13 am

Thank you very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 567
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 10 guests

cron