TryStrToFloat converts the wrong string

I am using TryStrToFloat to convert strings to double variables. Everything works fine until the line looks like "21e". I get conversion result 21.

It seems to me that the compiler considers "21e" as number 21e0. Line 21e1 gives the result 210. When I use the conversion of the Val function, it works better. String '21e' gives an error, but now '21e1' gives 210, '21e-1' gives the number 2.1, etc.

How to perform the conversion. Should I find the letter ā€œeā€ in the text or some simple conversion method?

+5
source share
1 answer

The documentation says:

Use TryStrToFloat to convert the string 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. Mantissa consists of an ā€œEā€ or ā€œeā€ followed by an optional sign (+ or -) and an integer. Leading and trailing spaces are ignored.

Your entry does not meet the conditions and therefore should be considered as an error.

You did not say so explicitly, but I assume that you are claiming that:

 TryStrToFloat('21e', val) 

returns True . If so, this is a bug that should be reported to Embarcadero. If you need to get around this, I suggest you create your own function that detects this case and processes it correctly.

On the other hand, if this function call returns False , the function behaves as designed, and your error is to read the value in val .

Update

I can confirm that TryStrToFloat('21e', val) returns True . I tested the XE7 update 1. I sent the following Embarcadero error report: https://quality.embarcadero.com/browse/RSP-9814

+8
source

Source: https://habr.com/ru/post/1208512/


All Articles