Groovy范围为0.5步长 [英] Groovy range with a 0.5 step size

查看:191
本文介绍了Groovy范围为0.5步长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy中用于指定整数范围和它们之间的0.5个步骤的最优雅的方法是什么?例如:1,1.5,2,2.5,3,3.5,4

编辑:澄清:作为最终结果,我需要一个范围在Grails约束中使用的对象。尽管我想列表也可以。

解决方案

我能看到的最好方法是使用step命令。



即p

$ $ p $ code
1.step(4,0.5){print $ it}

会打印出来:1 1.5 2.0 2.5 3.0 3.5


What's the most elgant way in Groovy to specify a range of integers and the 0.5 steps between them? e.g.: 1, 1.5, 2, 2.5, 3, 3.5, 4

Edit: To clarify: As an end result I need a range object for use in a Grails constraint. Though I suppose a list would be OK too.

解决方案

Best way I can see is using the step command.

i.e.


    1.step(4, 0.5){ print "$it "}

would print out: "1 1.5 2.0 2.5 3.0 3.5"

这篇关于Groovy范围为0.5步长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆