请问round函数是个什么运算原理,为什么2.5 是 2,3.5 确实4 呢
round函数
round函数的文档里面有解释这个问题:
Note that for rounding off a 5, the IEC 60559 standard is expected to be used, ‘go to the even digit’. Therefore round(0.5) is 0 and round(-1.5) is -2. However, this is dependent on OS services and on representation error (since e.g. 0.15 is not represented exactly, the rounding rule applies to the represented number and not to the printed number, and so round(0.15, 1) could be either 0.1 or 0.2).
是朝着偶数round,所以会这样,
我想你需要的是signif(x,digits=1).
Note that for rounding off a 5, the IEC 60559 standard is expected to be used, ‘go to the even digit’. Therefore round(0.5) is 0 and round(-1.5) is -2. However, this is dependent on OS services and on representation error (since e.g. 0.15 is not represented exactly, the rounding rule applies to the represented number and not to the printed number, and so round(0.15, 1) could be either 0.1 or 0.2).
是朝着偶数round,所以会这样,
我想你需要的是signif(x,digits=1).
前两天使用round()也遇到类似问题,之后才参考其它相关函数。
??ceiling
??floor
??trunc
??signif
??sprintf