Delphi isNumber
Czy istnieje metoda w Delphi sprawdzająca, czy łańcuch znaków jest liczbą bez wywoływania wyjątku?
Its for int parsing.
I wyjątek zostanie wywołany, jeśli ktoś użyje
try
StrToInt(s);
except
//exception handling
end;
10 answers
var
s: String;
iValue, iCode: Integer;
...
val(s, iValue, iCode);
if iCode = 0 then
ShowMessage('s has a number')
else
ShowMessage('s has not a number');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-08 05:16:06
Function TryStrToInt (const s: string; out Value: Integer): Boolean;
TryStrToInt zamienia łańcuch S, który reprezentuje liczbę całkowitą w notacji dziesiętnej lub szesnastkowej, na liczbę, która jest przypisana do wartości. Jeśli S nie reprezentuje poprawnej liczby, TryStrToInt zwraca false; w przeciwnym razie TryStrToInt zwraca true.
Aby przyjąć wartości dziesiętne, ale nie szesnastkowe w ciągu wejściowym, możesz użyć kodu w następujący sposób:
function TryDecimalStrToInt( const S: string; out Value: Integer): Boolean;
begin
result := ( pos( '$', S ) = 0 ) and TryStrToInt( S, Value );
end;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-10 05:02:48
Spróbuj tej funkcji StrToIntDef()
Z pomocy
Konwertuje łańcuch reprezentujący liczbę całkowitą (dziesiętną lub szesnastkową) na liczbę z domyślną wartością błędu.
Pascal
function StrToIntDef(const S: string; Default: Integer): Integer;
Edit
Właśnie sprawdzałem źródło TryStrToInt()
funkcji w Delphi 2007. JeĹ " li Delphi 7 nie posiada tej funkcji, moĹźesz napisaÄ ‡ tak. To tylko dopracowany kod do da-soft odpowiedz
function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
E: Integer;
begin
Val(S, Value, E);
Result := E = 0;
end;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-03 10:56:41
Dla starszych wersji Delphi z delphi 5 help przykład:
uses Dialogs;
var
I, Code: Integer;
begin
{ Get text from TEdit control }
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-03 10:17:31
W delphi 7 można skorzystać z procedury Val
. Z pomocy:
Jednostka: System
składnia Delphi: procedure Val(S; var V; var Code: Integer);
S jest wyrażeniem typu string; musi to być sekwencja znaków, które tworzą podpisaną liczbę rzeczywistą.
V jest zmienną typu całkowitego lub rzeczywistego. Jeśli V jest zmienną typu integer, to S musi tworzyć liczbę całkowitą.
Kod jest zmienną typu Integer.
Jeśli łańcuch jest Nieprawidłowy, indeks znaku jest przechowywany w Kod; w przeciwnym razie kod jest ustawiony na zero. Dla zakończonego znakiem null pozycja błędu zwracana w kodzie jest o jedną większą od rzeczywistego indeksu opartego na Odrze znaku błędu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 07:24:16
Użyj tej funkcji
function IsNumber(N : String) : Boolean;
var
I : Integer;
begin
Result := True;
if Trim(N) = '' then
Exit(False);
if (Length(Trim(N)) > 1) and (Trim(N)[1] = '0') then
Exit(False);
for I := 1 to Length(N) do
begin
if not (N[I] in ['0'..'9']) then
begin
Result := False;
Break;
end;
end;
End;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-26 12:44:47
W niektórych językach separatory dziesiętne są różne (np. '."jest używany w języku angielskim i", " jest używany w języku rosyjskim). W tych przypadkach do konwersji ciągu znaków na liczbę rzeczywistą proponuje się następującą procedurę:
function TryStrToFloatMultylang(const S : String; out Value : Extended) : Boolean;
var
dc : char;
begin
Result := false;
dc := DecimalSeparator;
DecimalSeparator := '.';
try
Result := TryStrToFloat(S, Value);
except
DecimalSeparator := ',';
Result := TryStrToFloat(S, Value);
end;
DecimalSeparator := dc;
end;
Update
Jak wspomniał @Pep TryStrToFloat catch wyjątki, ale zwraca wartość logiczną. Więc prawidłowy kod to:
function TryStrToFloatMultylang(const S : String; out Value : Extended) : Boolean;
var
dc : char;
begin
Result := false;
dc := DecimalSeparator;
DecimalSeparator := '.';
Result := TryStrToFloat(S, Value);
if not Result then begin
DecimalSeparator := ',';
Result := TryStrToFloat(S, Value);
end;
DecimalSeparator := dc;
end;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-03 17:26:34
XE4 i nowsze:
for ch in s do
TCharacter.IsNumber(ch);
Nie zapomnij:
uses System.Character
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-31 09:21:49
Podczas korzystania z procedury
Val (s, i, iCode);
I ustaw wartość xd ....
Val ('xd', i, iCode)
W rezultacie otrzymujemy: 13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-01 08:40:55
Standardowe warianty jednostek
function VarIsNumeric(v:Variant):Boolean
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-18 06:01:10