迅速不可靠的圆形()方法到小数点2

发布于 2025-02-01 10:55:20 字数 507 浏览 3 评论 0原文

我正在尝试使用swift rough()方法来计算两个双打(距离,速度)到2个小数点位置之间的时间,但是在某些情况下它不可靠,我最终得到了58.000000001之类的东西。我试图通过再次舍入来解决此问题,但即使在更大的数字上也不起作用,例如734.00000001 有没有办法修复此或另一种方法来计算两个双打之间的时间?

var totalTime: Double = 0
for 0...100 {
    let calcTime  = 35.3 / 70
    let roundedTime = round(calcTime * 100) / 100.0
    print("TIME === \(roundedTime)")
    totalTime += round(totalTime * 100) / 100 + roundedTime // round again to clamp the extra zero's
    print("Total time \(totalTime)")
} 

I'm trying to calculate the time between two doubles (distance, speed) to 2 decimal places using swift round() method but there are instances where its unreliable and I end up with something like 58.000000001. I tried to hack fix this by rounding again but even that doesn't work on larger numbers eg 734.00000001 Is there a way to fix this or another way to calculate time between two doubles?

var totalTime: Double = 0
for 0...100 {
    let calcTime  = 35.3 / 70
    let roundedTime = round(calcTime * 100) / 100.0
    print("TIME === \(roundedTime)")
    totalTime += round(totalTime * 100) / 100 + roundedTime // round again to clamp the extra zero's
    print("Total time \(totalTime)")
} 

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文