Math.round(msgBody.length() / 160 + 0.5) 计算不正确?

发布于 2024-10-25 09:13:26 字数 577 浏览 1 评论 0原文

因此,我尝试动态计算给定消息长度将发送的短信数量。 SMS 消息被分成 160 字节(字符)块。我使用 MOD 160 <= 1 因为我需要考虑添加和减去文本。出于性能原因,我只想在边界附近进行 numMsgs 计算:0, 1, 160, 161, 320, 321, ...

问题是在 msgBody 长度为 160 时,舍入操作的计算结果为 2 (160/160 = 1 + 0.5 四舍五入)。在 160 + 1 的倍数处,它应该计算为下一个最大整数,因为任何 160 字节 + 1 字节都等于整个附加消息。

我通过使用 OR 运算符 and == 1 OR == 159 完成了这项工作。它正确地递增和递减,但它仅以 160 -1 的倍数递减,这是不正确的。

另外,我可以将 IF 逻辑放入外部逻辑中,以便在 MOD 计算结果为 0(零)时简单地减去 1,但这看起来很笨拙,我宁愿学习我可能从我的箭袋中丢失的模式:)

if (msgBody.length() % 160 <= 1) {

    numMsgs.setText(Math.round(msgBody.length() / 160 + 0.5));

}

So I'm trying to dynamically calculate the number of SMS messages that will be sent for a given message length. SMS messages are chunked into 160 byte (character) chunks. I use MOD 160 <= 1 because I need to account for adding and subtracting text. For performance reasons I only want to do the numMsgs calculations near the boundaries: 0, 1, 160, 161, 320, 321, ...

The problem is at a msgBody length of 160 the rounding operation evaluates to 2 (160/160 = 1 + 0.5 rounded up). At multiples of 160 + 1 it should evaluate to the next highest integer because any 160 bytes + 1 byte equals a whole additional message.

I have made this work by using an OR operator and == 1 OR == 159. It increments and decrements correctly, but it only decrements at multiples of 160 -1 which is not correct.

Also, I can put IF logic inside the outer logic to simply subtract 1 if the MOD evaluates to 0 (zero), but that seemed kludgey and I'd rather learn the pattern I may be missing from my quiver :)

if (msgBody.length() % 160 <= 1) {

    numMsgs.setText(Math.round(msgBody.length() / 160 + 0.5));

}

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

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

发布评论

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

评论(3

疾风者 2024-11-01 09:13:26

msgBody.length() / 160 是一个除法表达式,两边都是整数 - 因此它将执行整数除法。我怀疑这就是你做错的事情,但说实话,这并不完全清楚。 (您声称它没有正确评估,但您没有提供示例输入/输出/预期输出供我们查看。)

说实话,最简单的方法是像这样进行舍入:

numMsgs.setText((msgBody.length() + 159) / 160);

仍然执行整数除法,但是+159 意味着如果输入不完全是 160 的倍数,它将向上舍入。

我建议您可以执行该计算无论边界如何 - 但请确保您如果与现有值相比没有变化,则实际上不会更新 UI。它会非常便宜——一个整数加法和一个整数除法。

msgBody.length() / 160 is a division expressions with both sides being integers - so it will perform integer division. I suspect that's what you're doing wrong, but it's not entirely clear to be honest. (You claim it's not evaluating correctly, but you haven't given a sample input / output / expected output for us to look at.)

To be honest, the simplest approach is to round up like this:

numMsgs.setText((msgBody.length() + 159) / 160);

That's still performing integer division, but the +159 means it will round up if the input isn't exactly a multiple of 160.

I suggest you could perform that calculation regardless of the boundaries - but make sure you don't actually update the UI if there's no change compared with the existing value. It's going to be very cheap - one integer add and one integer divide.

仅冇旳回忆 2024-11-01 09:13:26

如果有消息,则至少会发送 1 条短信。
你想要

      (length / 160) + (length%160 > 0 ? 1 : 0)

If there is a message, then at least 1 SMS will be sent.
You want

      (length / 160) + (length%160 > 0 ? 1 : 0)
酒解孤独 2024-11-01 09:13:26

只要使用正确的公式,使用 int 进行加法和除法就足够了。取模可能比除法还要慢。但是,我不清楚您期望什么结果... (msgBody.length() + 159) / 160 还不够吗?

Just adding and dividing using int is enough with the right formula. Modulo is probably even slower than dividing. However, it's not clear to me what result you expect... (msgBody.length() + 159) / 160 isn't enough?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文