Friday, January 17, 2020

Re: How to know if a converted string to float is really a float number ?

On Fri, Jan 17, 2020 at 7:29 PM Ni Va <nivaemail@gmail.com> wrote:
>
> HI,
>
> According to str2float help, I have tried this :
>
> '3,3e+004'->substitute(',', '.', 'g')->str2float() which return 33000 number.
>
> According to good conversion in this case it is a float num but if I replace by cases where it is not a well form of num that no have to be converted.
>
>
> '3,3e+00.4'->substitute(',', '.', 'g')->str2float() which return ???? there is a dot char in exp...
> '3,3e+0O4'->substitute(',', '.', 'g')->str2float() which return ???? there is char 'O' out of zero
>
>
> How to prevent unnecessary conversion ?
>
> Thank you

I tried the same examples, and in these last two cases I get 3.3 which
is to be expected since the conversion stops as soon as an input
character is found which would make the input invalid for a Float,
i.e., str2float('3.3e+00.4') interprets '3.3e+00' and converts it to
3.3 ; and str2float('3.3e+0O4') interprets '3.3e+0' and converts it to
3.3 — in both of these cases, trailing garbage is silently
disregarded.

In the first ("valid") case I get 33000.0 which is the number
expressed as a Float, not an (integer) Number.

If you can devise a regular expression covering what your input can
legitimately be, you could wrap the whole sequence of methods in an =~
or similar conditional (see :help expr-=~).


Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXvacfF1WGvKXP8u8OH-NAKph_bQvM9YCKW_KoVDa8PduA%40mail.gmail.com.

No comments: