macvim - Vim Round floats to one decimal place -


i'm setting function in .vimrc (using macvim in particular, should universal vim in general) display file sizes (in bytes, kilobytes, , megabytes) in statusline. while function works quite without errors, it's giving me unexpected output! in hindsight, it's producing output should, not output want.

here's function:

" modified filesize() function shown here suit own preferences: " http://got-ravings.blogspot.com/2008/08/vim-pr0n-making-statuslines-that-own.htm function! statuslinefilesize()     let bytes = getfsize(expand("%:p"))     if bytes < 1024         return bytes . "b"     elseif (bytes >= 1024) && (bytes < 10240)         return string(bytes / 1024.0) . "k"     elseif (bytes >= 10240) && (bytes < 1048576)         return string(bytes / 1024) . "k"     elseif (bytes >= 1048576) && (bytes < 10485760)         return string(bytes / 1048576.0) . "m"     elseif bytes >= 10485760         return string(bytes / 1048576) . "m"     endif endfunction 

here's way works:

  1. if filesize less 1kb, output size in bytes integer
  2. if filesize between 1kb , 10kb, output size in kilobytes decimal
  3. if filesize between 10kb , 1mb, output size in kilobites integer
  4. if filesize between 1mb , 10mb, output size in megabytes decimal
  5. if filesize greater 10mb, output size in megabytes integer

the output produced steps 2 , 4 decimals six (6) places of precision. desired output have should decimals one (1) place of precision.

i've searched documentation round() , trunc() functions, round , truncate floats nearest whole number value, not have happen. i've searched google , stackoverflow solutions, of can find involves altering text in edit buffer or unrelated problems such rounding floats in java (!!!)

i'm preferably looking vim built-in function can this, la round({expr},{prec}) or trunc({expr},{prec}), if user defined function can provide sufficiently elegant solution i'm well. don't mind if output string, since i'm returning string statuslinefilesize() anyways!

use printf precision specifiers convert results strings instead of string:

return printf('%.1fm', bytes / 1048576) 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -