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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Ni Va
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

--
--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20b9a263-d9fa-4596-a044-7aeacd6787c8%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

Tony Mechelynck
On Fri, Jan 17, 2020 at 7:29 PM Ni Va <[hidden email]> 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJkCKXvacfF1WGvKXP8u8OH-NAKph_bQvM9YCKW_KoVDa8PduA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

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

Ni Va
Ok Tony so I will prevent with regular expression that fit to float number. It seems well optimized.
Thank you

Le vendredi 17 janvier 2020 20:28:48 UTC+1, Tony Mechelynck a écrit :
On Fri, Jan 17, 2020 at 7:29 PM Ni Va <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="MZSyCGaXDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">niva...@...> 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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/cf167866-db35-44ca-bb74-7a041911324a%40googlegroups.com.