将 up/off 舍入到最接近的 5

Round up/off to nearest 5

我正在尝试将数字列表四舍五入到最接近的 5 和最接近的 10。

示例:

1562
1706
1665
1378
1439

我创建此代码是为了将 up/off 舍入到最接近的 5:

:exe "%s/\d\d\d\d/\=substitute(submatch(0).'\.0', '.*', (round(submatch(0)/5)*5), 'g')/g"

在替换的第一部分,我想从子匹配值中添加一个浮点数 .0 到子匹配值。

预期结果:

1560
1705
1665
1380
1440

但是,它给出了尾随字符错误。 我做错了什么?

由于 / 作为除法运算符出现在替换字符串中,因此您需要为 :s 命令使用另一个分隔符。根据 documentation,这可以是任何其他单字节字符,但不能是字母数字字符、'\'、'"'' 或 '|'。

要将数字 n 舍入到最接近 k 的倍数,您可以执行以下操作:

(n + k/2) / k * k

将这些放在一起,命令可以是:

:%s!\v\d{4}!\=(submatch(0) + 2) / 5 * 5!