将秒值转换为小时分钟秒?
我一直在尝试将秒值(在 BigDecimal 变量中)转换为 editText 中的字符串,例如1 小时 22 分 33 秒"或类似的东西.
I've been trying to convert a value of seconds (in a BigDecimal variable) to a string in an editText like "1 hour 22 minutes 33 seconds" or something of the kind.
我试过了:
String sequenceCaptureTime = "";
BigDecimal roundThreeCalc = new BigDecimal("0");
BigDecimal hours = new BigDecimal("0");
BigDecimal myremainder = new BigDecimal("0");
BigDecimal minutes = new BigDecimal("0");
BigDecimal seconds = new BigDecimal("0");
BigDecimal var3600 = new BigDecimal("3600");
BigDecimal var60 = new BigDecimal("60");
(我有一个roundThreeCalc,它是以秒为单位的值,所以我尝试在这里转换它.)
(I have a roundThreeCalc which is the value in seconds so I try to convert it here.)
hours = (roundThreeCalc.divide(var3600));
myremainder = (roundThreeCalc.remainder(var3600));
minutes = (myremainder.divide(var60));
seconds = (myremainder.remainder(var60));
sequenceCaptureTime = hours.toString() + minutes.toString() + seconds.toString();
然后我将 editText 设置为 sequnceCaptureTime 字符串.但这没有用.它每次都强制关闭应用程序.我在这里完全超出了我的深度,非常感谢任何帮助.快乐编码!
Then I set the editText to sequnceCaptureTime String. But that didn't work. It force closed the app every time. I am totally out of my depth here, any help is greatly appreciated. Happy coding!
推荐答案
你应该有更多的运气
hours = roundThreeCalc.divide(var3600, BigDecimal.ROUND_FLOOR);
myremainder = roundThreeCalc.remainder(var3600);
minutes = myremainder.divide(var60, BigDecimal.ROUND_FLOOR);
seconds = myremainder.remainder(var60);
这将在每次除法后删除十进制值.
This will drop the decimal values after each division.
如果这不起作用,试试这个.(我只是写了测试了一下)
If that didn't work, try this. (I just wrote and tested it)
public static int[] splitToComponentTimes(BigDecimal biggy)
{
long longVal = biggy.longValue();
int hours = (int) longVal / 3600;
int remainder = (int) longVal - hours * 3600;
int mins = remainder / 60;
remainder = remainder - mins * 60;
int secs = remainder;
int[] ints = {hours , mins , secs};
return ints;
}
相关文章