1

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;
                                        }

enter image description here

pppery
  • 3,731
  • 22
  • 33
  • 46
TheHuman
  • 11
  • 1
  • How is the table in your database created? (Please show `SHOW CREATE TABLE ...` for it). – Luuk Feb 11 '22 at 12:46
  • why not simply round – nbk Feb 11 '22 at 12:47
  • But when you do `round` you still should not use `float`. – Luuk Feb 11 '22 at 12:49
  • 1
    You must use DECIMAL datatype (with according digits amount values) in the table, not FLOAT/DOUBLE. – Akina Feb 11 '22 at 12:52
  • Hi, thnks for you really fast reactions. CREATE TABLE `money_system` ( `uuid` varchar(255) DEFAULT NULL, `money_amount` double DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 – TheHuman Feb 11 '22 at 13:06

1 Answers1

0

You can just parse The double into a String. Now you split the string-double with "." in index 1. So you get the decimal places as number. Now you can parse the that into a number and must check if that number is smaller then 100.

Code:

//d is the double from the input
int decimal = Integer.parseInt(Double.toString(d).split(".")[1]);
if(decimal < 100) {
//number has 2 decimal places
} else {
//number has more then 2 decimal places
}

Or you can just round the number with Math.round(d).