Java:字符串标记器并分配给2个变量?

发布于 2024-08-22 04:28:34 字数 157 浏览 5 评论 0原文

假设我有一个时间 hh:mm(例如 11:22),并且我想使用字符串标记器来分割。但是,在分割后,我可以得到例如:11 和下一行 22。但是如何将 11 分配给变量名“hour”和另一个变量名“min”?

还有一个问题。如何对数字进行四舍五入?即使是 2.1 我希望它四舍五入为 3?

Let's say I have a time hh:mm (eg. 11:22) and I want to use a string tokenizer to split. However, after it's split I am able to get for example: 11 and next line 22. But how do I assign 11 to a variable name "hour" and another variable name "min"?

Also another question. How do I round up a number? Even if it's 2.1 I want it to round up to 3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无言温柔 2024-08-29 04:28:34

看看 使用 String.split() 分割字符串

Spmething 类似

String s[] = "11:22".split(":");;
String s1 = s[0];
String s2 = s[1];

And ceil用于四舍五入

使用 Math 查找数字的上限。单元格

Have a look at Split a string using String.split()

Spmething like

String s[] = "11:22".split(":");;
String s1 = s[0];
String s2 = s[1];

And ceil for rounding up

Find ceiling value of a number using Math.ceil

锦欢 2024-08-29 04:28:34

将数字四舍五入并不难。首先,您需要通过将其转换为 int 和 double 进行比较来确定它是否是整数。如果不匹配,则说明该数字不完整,因此可以将 int 值加 1 进行四舍五入。


// num is type double, but will work with floats too
if ((int)num != (double)num) {
    int roundedNum = (int)num + 1;
}

Rounding a number up isn't too hard. First you need to determine whether it's a whole number or not, by comparing it cast as both an int and a double. If they don't match, the number is not whole, so you can add 1 to the int value to round it up.


// num is type double, but will work with floats too
if ((int)num != (double)num) {
    int roundedNum = (int)num + 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文