-1

I am currently working on my own currency system and am now looking for a solution to limit the decimal places in the argument to solve this problem.

Example

"/command username 4.555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555565555555555555555555555555555555555555555555555555555555555555555555555551"

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (sender instanceof Player) {
            Player player = (Player) sender;

            if (args.length == 0) {
                player.sendMessage(Main.prefix + "§7/Bezahlen §8(§aSpieler§8) §8(§aBetrag§8)");
                player.sendMessage(Main.prefix + "§7Dein Kontostand: §a" + moneyapi.getMoney(player.getUniqueId()));
                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]) >= 1) {
                                if (target != null) {
                                    if (target != player) {
                                        if (moneyapi.hasEnough(player.getUniqueId(), Double.parseDouble(args[1]))) {
                                            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-" + args[1] + "§8)");
                                            target.sendMessage(Main.prefix + "§7Erhalten: §c" + player.getName().toString() + "§8(§a+" + args[1] + "§8)");
                                            return true;
                                        }
                                    } else {
                                        player.sendMessage(Main.prefix + "§7Du kannst dir kein Geld senden!");
                                        return false;
                                    }
                                } else {
                                    player.sendMessage(Main.prefix + "§7Dieser Spieler ist nicht online! " + "§8(§a" + args[0] + "§8)");
                                    return false;
                                }
                            } else {
                                player.sendMessage(Main.prefix + "§7Bitte gebe einen gültigen Betrag ein!");
                                return false;
                            }
                        } else {
                            player.sendMessage(Main.prefix + "§7Bitte gebe einen gültigen Betrag ein!");
                            return false;
                        }
                    }
                }
            }
        } else {
            sender.sendMessage(Main.consoleblocker);
            return false;
        }
        return false;
    }
}

Could you guys possibly help me with this?

2 Answers2

0

You can check the amount of decimals on a double by using BigDecimal.valueOf(value).scale() > 2 where value is the double your checking. (2 in this case is the amount of decimal places max, you can change this to whatever number)

ElmoPog
  • 11
  • 3
  • What value do I enter to no longer have a decimal place? The value "0" or "-1" doesn't seem to help and I haven't found the right one yet, @ElmoPog – Displayname Feb 11 '22 at 00:37
0

There is multiple way to limit the decimal when showing the value.

  1. Number format. This is good if you want to limit with minimal and maximal amount of char for integers and fractions. Example:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);

Then, to show value, use: nf.format(amount).

  1. String format. Easiet way :
String.format("%.3f", amount);
Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • Hi, thanks for your tip. I have installed it on me and it works really well. However, I have not found a correct answer whether you can use this function with doubles, @elikill58 – Displayname Feb 10 '22 at 17:18
  • Both works with double, float of all value like that – Elikill58 Feb 10 '22 at 17:21