Monday, November 26, 2018

Re: str2float() issue

Am 26.11.2018 um 14:38 schrieb Vladimir Stenbock:
>
>> If you want to convert strings of hexadecimal (0xa), octal (0755),
>> binary (0b11110000) to integers, you can use unary + operator.
>>
>> :echo +'0xa'
>> => 10
>> :echo +'0755'
>> => 493
>> :echo +'0b11110000'
>> => 240
>>
>> P.S.
>> Vim script can treat an exponential notation, but unary + operator can
>> not convert it.
>>
>> :echo 1.0e3
>> => 1000.0
>> :echo +'1.0e3'
>> => 1
>>
>
> Yes, you exactly describe my pain. ))
>
> So my 'prefix'-dream is: 0x (hexadecimal, base 16), 0o (octal, base 8), 0b (binary, base 2)
>
> echo str2float('011') str2float('0x11') str2float('011e1') str2float('0b11') str2float('0o11')
> and get:
>
> 11.0 17.0 110.0 3.0 9.0
>
> instead of:
>
> 11.0 17.0 110.0 0.0 0.0
>
> in present time

Maybe use eval() to "normalize" the input string (given you can make sure that {str} isn't arbitrary user input)?

func! Str2float(str)
return str2float(string(eval(a:str)))
endfunc

But it's less permissive than str2float() for usual floating point numbers, eg '1e5' is not accepted.

--
Andy

--
--
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.
For more options, visit https://groups.google.com/d/optout.

No comments: