我很惊讶地发现R没有附带一个方便的函数来检查数字是否为整数.
is.integer(66) # FALSE
Run Code Online (Sandbox Code Playgroud)
在帮助文件警告:
is.integer(x)不测试是否x包含整数!为此,请使用round,如is.wholenumber(x)示例中的函数 .
该示例将此自定义函数作为"解决方法"
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol
is.wholenumber(1) # is TRUE
Run Code Online (Sandbox Code Playgroud)
如果我必须编写一个函数来检查整数,假设我没有阅读上面的注释,我会编写一个函数,它会像
check.integer <- function(x) {
x == round(x)
}
Run Code Online (Sandbox Code Playgroud)
我的方法会在哪里失败?如果你穿着我的假想鞋,你的工作会是什么?
我有以下代码来计算一定的百分比:
var x = 6.5;
var total;
total = x/15*100;
// Result 43.3333333333
Run Code Online (Sandbox Code Playgroud)
我想要的结果是确切的数字43,如果总数43.5应该四舍五入到44
有没有办法在JavaScript中这样做?
可能重复:
C++中float的round()
我有一个双(称为x),意味着55,但实际存储为54.999999999999943157,我刚才意识到.
所以,当我这样做
double x = 54.999999999999943157;
int y = (int) x;
Run Code Online (Sandbox Code Playgroud)
y = 54而不是55!
这困惑了我很久了.如何让它正确圆?
我知道Python//向负无穷大舍入,而在C++中则/是截断,向0舍入。
到目前为止,这是我所知道的:
|remainder|
-12 / 10 = -1, - 2 // C++
-12 // 10 = -2, + 8 # Python
12 / -10 = -1, 2 // C++
12 // -10 = -2, - 8 # Python
12 / 10 = 1, 2 // Both
12 // 10 = 1, 2
-12 / -10 = 1, - 2 // Both
= 2, + 8
C++:
1. m%(-n) == m%n
2. -m%n == -(m%n)
3. (m/n)*n …Run Code Online (Sandbox Code Playgroud) 我们如何在php中将数字四舍五入到最接近的10?
说我有23,我会使用什么代码将其四舍五入30?
我需要以下结果
100.12 -> 100.00
100.44 -> 100.00
100.50 -> 101.00
100.75 -> 101.00
Run Code Online (Sandbox Code Playgroud)
.round()还是.setScale()?我该怎么做?
我正在创建一个程序,由于不需要解释的原因,需要将float转换为字符串以使用len()进行计数.但是,str(float(x))导致x在转换为字符串时被舍入,这会抛出整个事物.有谁知道它的修复?如果你想知道,这是使用的代码:
len(str(float(x)/3))
Run Code Online (Sandbox Code Playgroud) 我怎样才能像这样舍入值:
1.1 => 1
1.5 => 2
1.9 => 2
Run Code Online (Sandbox Code Playgroud)
__CODE__没有帮助我.有任何想法吗?
我正在编写一个绘制数据的函数.我想为y轴指定一个很好的圆数max,它大于数据集的最大值.
具体来说,我想要一个foo执行以下功能的函数:
foo(4) == 5
foo(6.1) == 10 #maybe 7 would be better
foo(30.1) == 40
foo(100.1) == 110
Run Code Online (Sandbox Code Playgroud)
我已经到了
foo <- function(x) ceiling(max(x)/10)*10
Run Code Online (Sandbox Code Playgroud)
舍入到最接近的10,但这对任意舍入间隔不起作用.
在R中有更好的方法吗?
我试图找到在网站上构建寻呼机的总页数(所以我希望结果是一个整数.我得到一个记录列表,我想分成10页/页(页数)
当我这样做:
list.Count() / 10
Run Code Online (Sandbox Code Playgroud)
要么
list.Count() / (decimal)10
Run Code Online (Sandbox Code Playgroud)
而且list.Count() =12,我得到了结果1.
我将如何编码,以便2在这种情况下得到它(其余部分应该总是添加1)
rounding ×10
c# ×2
c++ ×2
python ×2
r ×2
bigdecimal ×1
integer ×1
java ×1
javascript ×1
operators ×1
php ×1
python-3.x ×1
string ×1