ExtractNumber
function ExtractNumber(const S: string): Double;
Example
procedure ScriptEvent(var Value: variant);
begin
Value := ExtractNumber('Weight: -12.50 kg'); // -12.5 with a dot-decimal system locale
end;
Usage
ExtractNumber filters selected ASCII numeric characters from text and parses the result as a system-locale Double.
Parameters
| Name | Type | Description |
|---|---|---|
S | string, const | Source text. Only ASCII 0..9, the first dot, and a leading +/- according to the collector rules can reach the parser. |
Returns
The parsed Double. If the source contains none of the accepted characters, it returns 0. A non-empty but syntactically incomplete collected string such as '-', '+', '.' or '-.' reaches StrToFloat and raises rather than returning zero.
Selection guidance
Use this helper only for deliberately permissive text whose expected punctuation and locale are controlled. For business amounts, validate one complete token and use an explicit known format/locale rather than relying on character extraction.
Additional Technical Info
ExtractNumber scans arbitrary text, keeps a small subset of ASCII numeric characters, then parses the synthesized text as a Double using the Windows system-default locale.
It is a permissive character collector, not a tokenizer and not a robust localized-number parser. Characters between digits are discarded, so unrelated numeric fragments can be joined. The example is fictional and source-reviewed only.
Implementation
ExtractNumberStringscansSfrom left to right.- Every ASCII digit is appended, wherever it occurs.
- The first
.is appended; later dots are discarded. +or-is appended only while the output count is still zero.- All other characters are discarded.
- Empty collected text returns zero. Otherwise
vxStrToFloatcreatesTFormatSettingsforLOCALE_SYSTEM_DEFAULTand calls DelphiStrToFloat. - The parsed
Extendedhelper result is narrowed to this function'sDoublereturn.
Examples of the collector
| Source | Collected text | Consequence |
|---|---|---|
'abc1def2' | '12' | Numeric fragments are concatenated. |
'1e3' | '13' | Exponent notation is not recognized. |
'1,234.56' | '1234.56' | Comma is discarded. |
'1.234,56' | '1.23456' | European grouping/decimal meaning is not preserved. |
'abc-12' | '-12' | A sign is accepted before the first retained character, even after discarded prefix text. |
'.-5' | '.5' | The dot consumes the initial position, so the later sign is discarded. |
Locale and format quirks
- The collector always emits a literal dot, but parsing uses system-default decimal and thousands separators. On a comma-decimal system, a dot can be rejected or interpreted differently.
- Parenthesized negatives, currency semantics, grouping validation, hexadecimal, exponent notation, NaN and infinity are not supported as structured forms.
- Unicode digits outside ASCII
0..9are discarded. - No trim or single-token boundary exists. IDs, dates, units and multiple amounts can collapse into one misleading number.
- Overflow, malformed collected text and system-locale ambiguity propagate as Delphi conversion errors.
Related entries
ConvertHtmlHexToTColorparses a fixed hexadecimal shape rather than filtering decimal text.
External references
Created 2026-07-15