gpt4 book ai didi

java - 去除除法中的小数

转载 作者:行者123 更新时间:2023-12-01 22:47:49 26 4
gpt4 key购买 nike

在我最近开发的一款游戏中,我创建了商店。当您购买商品并尝试出售时,售价会降至购买价格的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com