【问题标题】:Removing decimals from division result从除法结果中去除小数
【发布时间】:2014-08-04 03:25:32
【问题描述】:

我目前正在开发一款游戏,但遇到了一个小问题。 当玩家从商店购买物品时,我将其设置为以购买价格的 75% 出售。 但是当检查价值时(在出售物品之前),它会说,例如,“可以出售 115.0 金币” 如何从“115”中删除“.0”? 我还是编码新手,任何帮助表示赞赏。 (它也用括号显示,我也想删除它们。例如:“你可以卖这个(120.0)金币。”)

编辑:这是java。

  int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
    String ShopAdd = "";
    if (ShopValue >= 1000000000) {
        ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000000) + " billion)";
    } else if (ShopValue >= 1000000) {
        ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000) + " million)";
    } else if (ShopValue >= 1000) {
        ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000) + "k)";
    } else if (ShopValue >= 100) {
        ShopAdd = " (" + Math.floor(ShopValue*.75 / 1) + ")";
    } else if (ShopValue >= 10) {
        ShopAdd = " (" + Math.floor(ShopValue*.75 / 1) + ")";
    }  
    c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}

}

【问题讨论】:

  • 你用的是什么编程语言?
  • @aflasin 给你答案:Math.floor() 返回一个双精度值,将显示为整数,使用 ((int) Math.floor(..) )。

标签: decimal division


【解决方案1】:

简单的方法是使用整数类型转换

(int)Math.floor(ShopValue*.75 / 1)

【讨论】:

    【解决方案2】:

    Math.floor 正在返回 double 参数,因此您必须先将其转换为 int,然后再将其提供给 ShopAdd string

    【讨论】:

    • 我该怎么做? (对不起,我还是个新手,尝试了很多东西。)
    • int flooredNumber = (int)Math.floor(ShopValue*.75 / 1000000000); shopAdd = flooredNumber + " billion"; 就这样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多