【问题标题】:What is wrong with this simple code? Please someone. I tried online IDE and on my eclipse, but I get same output [duplicate]这个简单的代码有什么问题?请某人。我尝试了在线 IDE 和我的 Eclipse,但我得到了相同的输出 [重复]
【发布时间】:2016-09-22 20:35:18
【问题描述】:

我创建了这个简单的硬币转换,但由于某种原因,我的打印输出表现得很奇怪,它不应该输出 12995 吗?

这是我的代码:

public static void main(String[]args)
{
    double x=129.95;
    int y= (int)(x*100);
    System.out.println(y);
}

输出:12994

【问题讨论】:

  • 欢迎来到 StackOverFlow。请遵守发帖规则stackoverflow.com/help/how-to-ask 例如,不要在标题中加上“紧急”,这非常具有攻击性。那么你的代码没有文档化,给出的信息也不够。
  • 这可能有点无关紧要,但是为什么问题会受到反对,我知道 stackoverflow 充满了专业顾问,但这种问我们问题的动力不会。
  • 感谢您的所有帮助。我收入低,能联系到您的专业人士对我来说意义重大。感谢您的慷慨。
  • 不要放弃网站。您将获得真正问题的帮助。

标签: java debugging


【解决方案1】:

使用 Math.round 将为您提供预期的答案。有关浮点的更多信息,您可以参考帖子中的 cmets。

public static void main(final String[] args) {
    double x = 129.95;
    int y = (int) Math.round(x * 100);
    System.out.println(y);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多