Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/8591d5316b990fc4d7d5 to your computer and use it in GitHub Desktop.
Save mattn/8591d5316b990fc4d7d5 to your computer and use it in GitHub Desktop.
diff -r 3790fb70e04c runtime/doc/eval.txt
--- a/runtime/doc/eval.txt Tue Apr 21 19:10:49 2015 +0200
+++ b/runtime/doc/eval.txt Mon Apr 27 10:55:34 2015 +0900
@@ -1935,7 +1935,7 @@
repeat( {expr}, {count}) String repeat {expr} {count} times
resolve( {filename}) String get filename a shortcut points to
reverse( {list}) List reverse {list} in-place
-round( {expr}) Float round off {expr}
+round( {expr} [, {digits}]) Float round off {expr} with {digits}
screenattr( {row}, {col}) Number attribute at screen position
screenchar( {row}, {col}) Number character at screen position
screencol() Number current cursor column
@@ -5075,18 +5075,24 @@
If you want a list to remain unmodified make a copy first: >
:let revlist = reverse(copy(mylist))
-round({expr}) *round()*
+round({expr} [, {digits}]) *round()*
Round off {expr} to the nearest integral value and return it
as a |Float|. If {expr} lies halfway between two integral
values, then use the larger one (away from zero).
{expr} must evaluate to a |Float| or a |Number|.
+ If {digits} must evaluate to a |Float| or a |Number|.
+ When the {digits} argument is given then the result should be
+ rounded by number of {digits}. negative value is allowed.
+ value, it works as
Examples: >
echo round(0.456)
< 0.0 >
echo round(4.5)
< 5.0 >
echo round(-4.5)
-< -5.0
+< -5.0 >
+ echo round(4.583, 2)
+< -4.58
{only available when compiled with the |+float| feature}
screenattr(row, col) *screenattr()*
diff -r 3790fb70e04c src/eval.c
--- a/src/eval.c Tue Apr 21 19:10:49 2015 +0200
+++ b/src/eval.c Mon Apr 27 10:55:34 2015 +0900
@@ -8258,7 +8258,7 @@
{"resolve", 1, 1, f_resolve},
{"reverse", 1, 1, f_reverse},
#ifdef FEAT_FLOAT
- {"round", 1, 1, f_round},
+ {"round", 1, 2, f_round},
#endif
{"screenattr", 2, 2, f_screenattr},
{"screenchar", 2, 2, f_screenchar},
@@ -16412,11 +16412,19 @@
typval_T *rettv;
{
float_T f;
+ int error = FALSE;
rettv->v_type = VAR_FLOAT;
- if (get_float_arg(argvars, &f) == OK)
- rettv->vval.v_float = vim_round(f);
- else
+ if (get_float_arg(&argvars[0], &f) == OK) {
+ if (argvars[1].v_type != VAR_UNKNOWN) {
+ long n = get_tv_number_chk(&argvars[1], &error);
+ if (!error) {
+ float fn = pow(10.0, n);
+ rettv->vval.v_float = vim_round(f * fn) / fn;
+ }
+ } else
+ rettv->vval.v_float = vim_round(f);
+ } else
rettv->vval.v_float = 0.0;
}
#endif
diff -r 3790fb70e04c src/testdir/test65.in
--- a/src/testdir/test65.in Tue Apr 21 19:10:49 2015 +0200
+++ b/src/testdir/test65.in Mon Apr 27 10:55:34 2015 +0900
@@ -60,6 +60,11 @@
:$put =printf('%g', round(0.456))
:$put =printf('%g', round(4.5))
:$put =printf('%g', round(-4.50))
+:$put =printf('%g', round(5.127,-1))
+:$put =printf('%g', round(5.127,0))
+:$put =printf('%g', round(5.127,1))
+:$put =printf('%g', round(5.127,2))
+:$put =printf('%g', round(5.127,3))
:$put ='sqrt'
:$put =printf('%g', sqrt(100))
:echo sqrt(-4.01)
diff -r 3790fb70e04c src/testdir/test65.ok
--- a/src/testdir/test65.ok Tue Apr 21 19:10:49 2015 +0200
+++ b/src/testdir/test65.ok Mon Apr 27 10:55:34 2015 +0900
@@ -43,6 +43,11 @@
0.0
5.0
-5.0
+10.0
+5.0
+5.1
+5.13
+5.127
sqrt
10.0
str2float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment