I am working on my first own money system. So far, except for one thing, everything is going great.
I currently can't find a solution to block from the 3rd decimal place of my argument if it is "0".
Example: /pay (Player) (1.11000000000000...1)
With (!(BigDecimal.valueOf(Double.parseDouble(args[1])).scale() > 2))
, every number except 0 is blocked from the 3rd decimal place on.
With NumberFormat
the amounts are displayed correctly to all players, but in the database then the crooked amount is entered.
Do you have any idea how I can block this?
public class Command_Geld implements CommandExecutor {
Api_Money moneyapi = new Api_Money();
public boolean isDouble(String isDouble) {
try {
Double.parseDouble(isDouble);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
if (args.length == 0) {
player.sendMessage(Main.prefix + "§7/Bezahlen §8(§aSpieler§8) §8(§aBetrag§8)");
player.sendMessage(Main.prefix + "§7Deine HardwareWallet: §a" + nf.format(moneyapi.getMoney(player.getUniqueId())) + " §7Satoshis");
return false;
} else {
Player target = Bukkit.getPlayer(args[0]);
if (args.length == 1) {
player.sendMessage(Main.prefix + "§7/Bezahlen §8(§aSpieler§8) §8(§aBetrag§8)");
return false;
} else {
if (args.length == 2) {
if (isDouble(args[1])) {
if (Double.parseDouble(args[1]) >= 0.01) {
if (target != null) {
if (target == player) {
if (!(BigDecimal.valueOf(Double.parseDouble(args[1])).scale() > 2)) {
moneyapi.removeMoney(player.getUniqueId(), Double.parseDouble(args[1]));
moneyapi.addMoney(target.getUniqueId(), Double.parseDouble(args[1]));
player.sendMessage(Main.prefix + "§7Bezahlt: §a" + target.getName().toString() + "§8(§c-" + nf.format(Double.parseDouble(args[1])) + "§8)");
target.sendMessage(Main.prefix + "§7Erhalten: §c" + player.getName().toString() + "§8(§a+" + nf.format(Double.parseDouble(args[1])) + "§8)");
return true;
} else {
player.sendMessage(Main.prefix + "§7Dir fehlen §8(§c" + moneyapi.getMoneyDifference(player.getUniqueId(), Double.parseDouble(args[1])) + "§8)");
return false;
}