- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在从文本文件或复制/粘贴到终端中输入一组数字。下面的复制/粘贴示例:
2.552789
2.919615
3.542125
4.212462
5.261249
6.320953
等等。这些数字都用一行分隔,并且都遵循这种格式seconds.hundredths of a second
。
我将数字列表输入数组并使用以下方法四舍五入到小数点后两位:
while
input != " "
array.push 0.0 + input.to_f.round(2)
input = gets.chomp
end
file_to_open = gets.chomp
array = File.open(file_to_open).readlines.each do |line|
input = file_to_open
while
input != ''
array.push 0.0 + input.to_f.round(2)
input = gets.chomp
end
我还让脚本使用以下方法从数组中选择每第 4 个项目:
module Enumerable
def every_nth(n)
(0... self.length).select{ |x| x%n == n-1 }.map { |y| self[y] }
end
end
最后,我使用一个 block 在每个数字前添加一个“0.”
array = array.map! {|word| "0.#{word}" }
当使用 print array
时,输出如下:
0.2.55 0.2.91 0.3.54 0.4.21 0.5.26 0.6.32.
当我到达 60 秒时,该列表会以秒为单位继续,而它应该变成分钟,例如:
0.61.54
0.63.78
我想将任何超过 60 秒的内容表示为 m.ss.hs
,即
“1.1.54”
“1.3.78”
我可以用模 % 60
来做到这一点,但我不确定如何处理整个数组。我在玩弄第一个小数点(分钟)前的数字作为变量,如果 秒数 % 60 == 0
,则将 1
添加到变量中,但这对 120
秒没有帮助,因为 120 % 60 == 0
也是如此。
我的代码可以运行到 60 秒,我希望它能将超过 60 秒的任何内容适本地转换为分钟。我找不到任何解释如何修改小数点后三位数组(而不是标准 h:m:s
格式)的内容。非常感谢任何和所有帮助。
最佳答案
这里是将您上面描述的格式中的秒数转换为minutes.seconds.hundredths
def display_minute(sec)
round_sec = sec.to_i
# Get the minute
m = round_sec / 60
# Get the remaining seconds
s = round_sec % 60
# The hundredth as integer
h = (sec - round_sec).round(2) * 100
h = h.to_i
# Combine result
[m, s, h].join('.')
end
此方法的一些结果:
display_minute(6.320953)
=> "0.6.32"
display_minute(61.320953)
=> "1.1.32"
display_minute(121.320953)
=> "2.1.32"
display_minute(185.320953)
=> "3.5.32"
关于ruby - 修改多小数点数组,秒转分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570107/
我试图找到一种方法来限制 Haskell,以便返回只有 2 个十进制名称的数字(例如 1.24) 我的代码是: sumSquares = sum . map (^ 2) 在 Haskell 中如何做到
我在向输入框添加停靠点验证时遇到一些问题。仅允许数字的限制(第二个条件)有效,但第一个条件也可能不存在。输入框中仍然没有出现句号和小数点。 function isNumberKey(evt) {
如何处理R中的p值? 我期望非常低的 p 值,例如: 1.00E-80 我需要-log10 -log10(1.00E-80) -log10(0) 是 Inf,但 Inf 也有四舍五入的感觉。 但似乎在
我的数据库中有一个包含货币字段的表。我已经为该货币字段创建了实体并创建了 decimal 属性。当该字段的值显示在我的 MVC3 View 上时,它在小数点后有四个零 0000,如下所示:5489.0
label.text = [NSString stringWithFormat:@"%.2f",final]; 上面的语句显示变量“final”中可用的浮点值,小数点后两位。 我想显示小数位数,具体取
有没有办法在 Qt 5 中绘制带有小数点大小的文本。我正在尝试使用 QFont::setPointSizeF() 但它似乎在我尝试过的任何平台(mac/linux/windows)上都不起作用,并且点
我有一个关于如何将基数为 10 的数字转换为 IEEE 754 浮点表示的示例 Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0 Normali
我有一个包含以下值的 CSV 文件: 3271.96;274;272;1;1;0;1;0.071690;0;0;0;0;0;0;1.753130;1.75;0;1.75; 但是当我用 Excel 打开
有人可以对 here 中的这些特殊字符示例给出更好的解释吗? ?或者提供一些更清楚的例子? (x) The '(foo)' and '(bar)' in the pattern /(foo) (bar
我编写了 change() 来帮助我将字符串转换为 double。 例如:如果字符串是“5.5”,我希望数字是5.5。如果字符串是“0.0”,我希望数字是0。另一个例子:“50”到50。 现在的问题是
我正在尝试确定 python float 与零进行比较时的精确度。请注意以下几点: 1e-323 == 0 > False 1e-324 == 0 > True 我认为阈值是 324 个小数点,至少对
我想使用 .ftr 文件来快速分析数百个表。不幸的是,我在十进制和千位分隔符方面遇到了一些问题,类似于 that post ,只是 read_feather 不允许 decimal=',', thou
在 SQL Server 2008 中,我正在运行一个报告,我需要在其中划分两列(称为 Completes 和 Prescreens,整数),然后转到 Excel并得到 Prescreens 除以 C
我有一个 NSString,我想将其转换为 NSDecimalNumber。该字符串是从服务器接收的,并且始终使用 en_US 语言环境进行格式化,例如 XXX.YYY 而不是 XXX,YYY。我想创
我是一名优秀的程序员,十分优秀!