Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-11

How to check if a string is a valid floating-point number.

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kjow

Hi all,

I need to know if a string is a valid floating-point number, so I tried this function:

function IsStrFloatNum(s: string): Boolean;
begin
  try
    // try to convert the string into a floatnumber
    StrToFloat(s);
    // if there is no error the result is true
    Result := True;
  except
    // if there is any error the result is false
    Result := False;
  end;
end;

(From: http://www.swissdelphicenter.ch/torry/showcode.php?id=304 )

Well, I get this error:

Debugger Exception Notification
project.exe raised exception class 'EConvertError' with message: "Cross" is an invalid float

I used this function some month ago in this project and it worked without problems, but now (some month after) I re-tried to re-build and re-use this function and I get this error.

Did I inadvertently change something?

Thank you!
Kjow

Reply by email

Michael Fuchs
to Kjow
Kjow wrote
Debugger Exception Notification project.exe raised exception class 'EConvertError' with message: "Cross" is an invalid float

This happens only if you run it in Lazarus. In this notification window should be a checkbox for ignoring this exception type while debugging.

hth
Michael

Reply by email

Michael Van Canneyt
to Kjow: I need to know if a string is a valid floating-point number, so I tried this function:…

You should use the default TryStrToFloat() function from the sysutils unit. It does exactly that, without exceptions.

The function result may change if the internationalization/localization settings have changed. e.g. if you switch from platform.

Secondly, please note that in the debugger you will always 'see' the exception before it is caught in the exception handler.

Michael.

Kjow wrote 42 lines
Hi all, I need to know if a string is a valid floating-point number, so I tried this function: function IsStrFloatNum(s: string): Boolean; begin try // try to convert the string into a floatnumber StrToFloat(s); // if there is no error the result is true Result := True; except // if there is any error the result is false Result := False; end; end; (From: http://www.swissdelphicenter.ch/torry/showcode.php?id=304 ) Well, I get this error: Debugger Exception Notification project.exe raised exception class 'EConvertError' with message: "Cross" is an invalid float I used this function some month ago in this project and it worked without problems, but now (some month after) I re-tried to re-build and re-use this function and I get this error. Did I inadvertently change something? Thank you! Kjow -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Kjow
to Michael Van Canneyt

2011/11/25 Michael Van Canneyt :

Show quoted text 11 lines
You should use the default TryStrToFloat() function from the sysutils unit. It does exactly that, without exceptions. The function result may change if the internationalization/localization settings have changed. e.g. if you switch from platform. Secondly, please note that in the debugger you will always 'see' the exception before it is caught in the exception handler. Michael.

Thank you both!

Reply by email

Santiago A.
to Kjow: I need to know if a string is a valid floating-point number, so I tried this function:…

El 25/11/2011 10:16, Kjow escribi?:

What about the standard pascal function "val"

function IsStrFloatNum(s: string): Boolean;
var
 dummyNumber:double;
 posError:integer;
begin
 val(s, dummyNumber, posError);
 result:=(posError=0);
end;

Time ago I came across a program written by someone that had to import data from an ascii file, and epending upon the contents of the line, select the kind of record to import. The initial programmer used exceptions. "I try this record, if it raises an exception then I try the other". I Changed it to a simple initial check of contents. The initial version took about 30 seconds, my version 3 seconds.

Lesson: Exceptions should be use to catch errors, not to common tests.

Show quoted text 41 lines
Hi all, I need to know if a string is a valid floating-point number, so I tried this function: function IsStrFloatNum(s: string): Boolean; begin try // try to convert the string into a floatnumber StrToFloat(s); // if there is no error the result is true Result := True; except // if there is any error the result is false Result := False; end; end; (From: http://www.swissdelphicenter.ch/torry/showcode.php?id=304 ) Well, I get this error: Debugger Exception Notification project.exe raised exception class 'EConvertError' with message: "Cross" is an invalid float I used this function some month ago in this project and it worked without problems, but now (some month after) I re-tried to re-build and re-use this function and I get this error. Did I inadvertently change something? Thank you! Kjow -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Regards santiago A.

Reply by email

silvioprog
to Santiago A.
var
  S: string;
  F: Double;
  R: Boolean;
begin
  S := 'A';
  R := TryStrToFloat(S, F);
end;
Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

Santiago A.
to Bart

El vie, 25-11-2011 a las 14:34 +0100, Bart escribi?:

Show quoted text 10 lines
On 11/25/11, Santiago A. < <mailto:>> wrote: > El 25/11/2011 10:16, Kjow escribi?: > > What about the standard pascal function "val" > Doesn't Val() use a hardcoded period (.) as decimalseparator? The StrToFloat() family uses (Default)FormatSettings AFAIK. Bart

Yes, it does. val uses dot as decimal separator In fact, I am Spanish and I use ',' as decimal separator, so I have such problem.

I remember I had used a similar function (in delphi). It deletes '.' (just in case it has thousands separators) then replaces ',' for '.', then applies the 'val'. Probably it could be checked faster scanning chars. But I found that function written and I didn't waste more time optimizing it, anyway it is wide long much better than rising exceptions.

By the way, I don't have a delphi near to check it. Is tryStrTofloat a fpc function or it is also a delphi function? If there is such function in delphi, someone gave up searching help too soon.

Regards
Santiago A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111128/d1b21c89/attachment.html>

Reply by email

silvioprog
to Santiago A.

2011/11/28 Santiago A. :
[...]

By the way, I don't have a delphi near to check it. Is tryStrTofloat a fpc function or it is also a delphi function? If there is such function in delphi, someone gave up searching help too soon.

After F1 on Delphi:
=============

Delphi Object and Component Reference
TryStrToFloat function

Converts a given string to a floating-point value with a boolean success code.

Unit

SysUtils

Category

floating point conversion routines

Delphi syntax:

function TryStrToFloat(const S: string; out Value: Extended): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Double): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Extended; const
FormatSettings: TFormatSettings): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Double; const
FormatSettings: TFormatSettings): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single; const
FormatSettings: TFormatSettings): Boolean; overload;

C++ syntax:

extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
Extended &Value);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, double &Value);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, float &Value);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
Extended &Value, const TFormatSettings FormatSettings);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
double &Value, const TFormatSettings FormatSettings);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, float
&Value, const TFormatSettings FormatSettings);

Description

Use TryStrToFloat to convert a astring, S, to a floating-point value. S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored.

The DecimalSeparator global variable defines the character that must be used as a decimal point. Thousand separators and currency symbols are not allowed in the string. If S doesn't contain a valid value, TryStrToFloat returns Default.

TryStrToFloat places the result in Value, and returns true if the conversion was successful, false otherwise.

The first three forms of TryStrToFloat are not thread-safe, because they use localization information contained in global variables. The second three forms of TryStrToFloat, which are thread-safe, refer to localization information contained in the FormatSettings parameter. Before calling the thread-safe forms of TryStrToFloat, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call GetLocaleFormatSettings.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email