如何在Groovy Range对象中做到这一点? [英] How to do this in Groovy Range Object?
问题描述
我想要0.1到2之间的数字(比如0.1,0.2,0.3,0.4等到2.0),因为我是个时髦人物,我决定 Range
会做这项工作,并带着这样的代码:
def a = 0.1..2
println a
但它的打印只有 [0.1,1.1]
as它的输出!所以我不能在 Range
中做到这一点?或通过语法错误?
在此先感谢。 解决方案
这不是微不足道的。范围对象有一个步骤方法,但这只允许整数步骤。
另一个解决方案是定义你自己的范围对象。如果一个列表也可以,你可以对一个数字使用step方法:
0.1.step(2,0.1){print$ it}
另一个解决方案:
(1 .. 20).collect {it / 10}
I want numbers from 0.1 to 2(such as 0.1,0.2,0.3,0.4 so on to 2.0), as I'm a groovy guy i decided Range
will do the job, and came with code like this:
def a = 0.1..2
println a
But its printing only [0.1, 1.1]
as its output! So i can't do this in Range
? or by syntax is wrong?
Thanks in advance.
that's not trivial. The range object has a step method, but this allows only for integer steps. Another solution could be to define your own range object.
If a list is also ok, you can use the step method on a number:
0.1.step(2, 0.1){ print "$it "}
another solution:
(1 .. 20).collect{it/10}
这篇关于如何在Groovy Range对象中做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!