java - 去除除法中的小数

标签 java decimal division

在我最近开发的一款游戏中,我创建了商店。 当您购买商品并尝试出售时,售价会降至购买价格的 75%。 购买 154 金币的元素后,商店会以 115.5 金币的价格购买,但你可以通过出售获得 115 金币。

我希望从“115.5”中删除“.5” 如有任何帮助,我们将不胜感激。

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

最佳答案

我只会使用Math.floor

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

如果您想对玩家更慷慨一点,您可以使用 Math.ceil ;-)

关于java - 去除除法中的小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106005/

相关文章:

java - cqengine 连接两个以上集合

java - 在 Java 中拆分和裁剪字符串数组

division - 如何对大量数字(bignums)实现长除法

java - 检查跨目录的文件分布

c# - 十进制与双倍! - 我应该在什么时候使用哪一个?

javascript - 在 Javascript 和 PHP 中十进制到 RGB

javascript - 如何将价格限制为小数点后两位

c++ - C++ 中的除法没有按预期工作

html - 透明盒子,穿过它后面的盒子

java - 使调度方法线程安全