Recent

Author Topic: TryStrToFloat  (Read 1349 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 300
TryStrToFloat
« on: September 09, 2024, 12:38:54 am »
Hmm, no message. Note there is nothing after '.'.
I assume it thinks strtofloat('0.') is 0. How to check then if string or decimal ?

Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFormatSettings;
  3.   x: Float;
  4. begin
  5.   x := NaN;
  6.   fs := FormatSettings;
  7.   fs.DecimalSeparator := '.';  
  8.  
  9.  
  10.   if not TryStrToFloat('0.',x, fs)then
  11.   begin
  12.     showmessage('foo');
  13.   end;        
  14.  
« Last Edit: September 09, 2024, 01:10:20 am by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

paweld

  • Hero Member
  • *****
  • Posts: 1217
Re: TryStrToFloat
« Reply #1 on: September 09, 2024, 06:36:07 am »
if the TryStrToFloat function returns TRUE, it means that the string was successfully converted to a number, and if FALSE, the conversion failed.
Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFormatSettings;
  3.   x: Double;
  4. begin
  5.   fs.DecimalSeparator := '.';
  6.   if TryStrToFloat('0.', x, fs) then
  7.     ShowMessage('It''s float: ' + FloatToStr(x))
  8.   else
  9.     showmessage('It'' string');
  10. end;          
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 15717
  • Censorship about opinions does not belong here.
Re: TryStrToFloat
« Reply #2 on: September 09, 2024, 06:54:07 am »
Should it not be '0.0';?
I mean it is not called a separator for nothing: it expects two decimal values either side of the dot.
« Last Edit: September 09, 2024, 06:59:45 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 300
Re: TryStrToFloat
« Reply #3 on: September 09, 2024, 08:03:33 am »
Should it not be '0.0';?
I mean it is not called a separator for nothing: it expects two decimal values either side of the dot.

Checking that OnKeyUp on TEdit control... Press backspace on something like '0.2'
lazarus 3.2-fpc-3.2.2-win32/win64

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 300
Re: TryStrToFloat
« Reply #4 on: September 09, 2024, 08:12:38 am »
test:
Code: Pascal  [Select][+][-]
  1. showmessage(FloatToStr(StrToFloat('0.'))); //--> 0
  2. showmessage(FloatToStr(StrToFloat('01')));//--> 1          
  3.  

Not very logical.
lazarus 3.2-fpc-3.2.2-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12364
Re: TryStrToFloat
« Reply #5 on: September 09, 2024, 09:20:44 am »
I mean it is not called a separator for nothing: it expects two decimal values either side of the dot.
Even good-old Turbo Pascal detects '0.' as 0, and '.1' as 0.1

There's nothing wrong with these numbers (go to the U.S., or look at an Americal Youtube math-related video, and you'll see them all over the place).
« Last Edit: September 09, 2024, 10:13:12 am by wp »

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: TryStrToFloat
« Reply #6 on: September 09, 2024, 10:11:21 am »
?!?!?!?
TryStrToFloat calls TextToFloat, which calls Val
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Uses Sysutils;
  3. Var
  4.   x:Double;
  5.   fs: TFormatSettings;
  6. begin
  7.   fs:=FormatSettings;
  8.   fs.DecimalSeparator:='.';
  9.   Writeln(TryStrToFloat('0.',x,fs),' = ',x);
  10. end.

Returns
Code: [Select]
TRUE =  0.0000000000000000E+000
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 1600
Re: TryStrToFloat
« Reply #7 on: September 09, 2024, 10:15:22 am »
The sysutil conversion functions all rely on the internal call to the val directive, which does a few interesting things. Basically it can parse everything that you can write into your pascal code as number constant, and even more:
Code: Pascal  [Select][+][-]
  1.   val('$FF', i, err);
  2.   val('%10', i, err);
  3.   val('&43', i, err);
  4.   val('1.', d, err);
  5.   val('.1', d, err);
  6.   val('3e4', d, err);

If you want only "normal" number notation, you should simply write your own function:
Code: Pascal  [Select][+][-]
  1. type
  2.   TNumberType = (ntNotANumber, ntInteger, ntFloat);
  3.  
  4. function isNumber(const AString: String): TNumberType;
  5. var
  6.   c: Char;
  7. begin
  8.   if Length(AString) = 0 then
  9.     Exit(ntNotANumber);
  10.   Result := ntInteger;
  11.   for c in AString do
  12.     if (c='.') and (Result=ntInteger) then
  13.       Result:=ntFloat
  14.     else if not (c in ['0'..'9']) then
  15.       Exit(ntNotANumber);
  16. end;

Which you can further customize to your liking (e.g. by adding a last if statement to rule out a . without followup)
« Last Edit: September 09, 2024, 10:17:38 am by Warfley »

Bart

  • Hero Member
  • *****
  • Posts: 5364
    • Bart en Mariska's Webstek
Re: TryStrToFloat
« Reply #8 on: September 09, 2024, 10:33:40 am »
The sysutil conversion functions all rely on the internal call to the val directive, which does a few interesting things. Basically it can parse everything that you can write into your pascal code as number constant, and even more:

And it can even parse enums.

Bart

jcmontherock

  • Sr. Member
  • ****
  • Posts: 263
Re: TryStrToFloat
« Reply #9 on: September 09, 2024, 05:25:30 pm »
"StrToFloatDef", "StrToIntDef" could also be used
Windows 11 UTF8-64 - Lazarus 3.4-64 - FPC 3.2.2

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 300
Re: TryStrToFloat
« Reply #10 on: September 12, 2024, 09:27:52 pm »
"StrToFloatDef", "StrToIntDef" could also be used

on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?
Code: Pascal  [Select][+][-]
  1.   fs := FormatSettings;
  2.   fs.DecimalSeparator := '.';
  3.  
  4.  
  5.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  6.   begin
  7.     Exit;
  8.   end;    
  9.  
  10.  
lazarus 3.2-fpc-3.2.2-win32/win64

dseligo

  • Hero Member
  • *****
  • Posts: 1372
Re: TryStrToFloat
« Reply #11 on: September 12, 2024, 09:48:37 pm »
"StrToFloatDef", "StrToIntDef" could also be used

on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?
Code: Pascal  [Select][+][-]
  1.   fs := FormatSettings;
  2.   fs.DecimalSeparator := '.';
  3.  
  4.  
  5.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  6.   begin
  7.     Exit;
  8.   end;    
  9.  
  10.  

I get 'exit' on my screen:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses SysUtils;
  4.  
  5. var
  6.   fs: TFormatSettings;
  7.  
  8. begin
  9.   fs := FormatSettings;
  10.   fs.DecimalSeparator := '.';
  11.  
  12.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  13.   begin
  14.     WriteLn('exit');
  15.     Exit;
  16.   end;
  17.  
  18.   WriteLn('end');
  19. end.

TRon

  • Hero Member
  • *****
  • Posts: 3271
Re: TryStrToFloat
« Reply #12 on: September 12, 2024, 10:19:24 pm »
on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?
Which is exactly working as advertised:

Quote
StrToFloatDef tries to convert the string S to a floating point value, and returns this value. If the conversion fails for some reason, the value Default is returned instead.
This tagline is powered by AI

dseligo

  • Hero Member
  • *****
  • Posts: 1372
Re: TryStrToFloat
« Reply #13 on: September 12, 2024, 11:12:59 pm »
on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?
Which is exactly working as advertised:

Quote
StrToFloatDef tries to convert the string S to a floating point value, and returns this value. If the conversion fails for some reason, the value Default is returned instead.

But he said it doesn't return default:
Quote
on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?

TRon

  • Hero Member
  • *****
  • Posts: 3271
Re: TryStrToFloat
« Reply #14 on: September 12, 2024, 11:25:17 pm »
But he said it doesn't return default:
That is not exactly 100% TS' words :)

Quote
on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?
Indeed: "it does not go into exit" and "it does nothing" (with the conclusion, no errors or exceptions).

Last part is covered by the documentation as linked.

The statement "it does nothing" can be analyzed as follows: the first line that gets executed when the condition is met is the line containing the exit statement. In the particular example of TS, the condition is true thus exit gets executed (as shown per your example as well). And indeed that does nothing (further) except for executing the exit statement (no idea what TS is expecting to happen).

The statement "it does not go into exit" deserves further explanation from TS, because the context is important there. An exit statement only exits a function (and if that function happens to be the main function it will exit the program). With the code as shown by TS it is impossible for TS to conclude that the exit statement is not executed.
« Last Edit: September 12, 2024, 11:28:49 pm by TRon »
This tagline is powered by AI

 

TinyPortal © 2005-2018