时间值数组的 Ruby 平均值 (fixnums)
想要获得平均持续时间,其中持续时间的格式为 1.day、3.months、2.weeks 格式。
# provided array
a = [1.day, 3.days, 1.week, 4.days]
# desired output
a.average = "3 days"
我尝试过的任何方法都会导致输出秒数。例如:
a.inject(:+) = "15 days"
a.inject(:+) / a.size = 324000
我已经查看了语言学宝石,但它只输出数字形式的值(三十二万四千)
Looking to get the average duration, where duration is in the form of 1.day, 3.months, 2.weeks format..
# provided array
a = [1.day, 3.days, 1.week, 4.days]
# desired output
a.average = "3 days"
Any way I have tried results in a number of seconds being the output.. for instance:
a.inject(:+) = "15 days"
a.inject(:+) / a.size = 324000
I've looked at the Linguistics gem, but it only outputs the value as a number (three hundred and twenty four thousand)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你坚持的话。根据需要进行舍入和/或截断。
If you insist. Round and/or truncate however you want.