Solved Math...

Discussion in 'Plugin Development' started by Meatiex, Jan 3, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Meatiex

    The below code is giving very long numbers when I input 2, 3, 99 and probably more...

    Code:
    double temp = 1;
    double price = ((temp  * 0.30) + temp) * input;
    I do the same math problem in my windows calculator and it turns out not being a super long number, is the java calculator broken?

    Heres my full code:
    Code:
        } else if (cmd.equals("buy")) {
            if (args.length >= 2) {
                String item = args[1];
                int ammount = 1;
                if (args.length == 3) ammount = Integer.parseInt(args[2]);
               
                double temp = EcoData.getPrice(item);
                double price = ((temp  * 0.30) + temp) * ammount;
               
                if (!(EcoData.getMoney(player.getName()) > price)) {
                    send(player, "&c&lInsufishent Funds");
                    return;
                }
               
                if (price != 0) {
                    send(player, "&2&lBought &7&l" + ammount + " " + item + "&2&l for $&7&l" + String.valueOf(price).replace(".0", ""));
                    player.setItemInHand(new ItemStack(Material.AIR));
                    EcoData.takeMoney(player.getName(), price);
                    player.getInventory().addItem(new ItemStack(Material.getMaterial(item), ammount));
                   
                } else send(player, "&c&lCan't Buy That Item");
            return;
        }
        }

    Any help is awesome :D
     
  2. Offline

    hexaan

    @Meatiex

    No :p

    Java doesn't round the number like your calculator does
     
  3. Offline

    teej107

    @Meatiex Java calculator is not broken. It is your logic that is. What are you trying to do exactly?
     
  4. Offline

    Meatiex

    double price = ((temp * 0.30) + temp) * input;
    temp = how much the item sells for
    input = how many items you want to buy
    * 0.30 = %30

    I want to add %30 to how much the item sells for
     
  5. Offline

    API_Tutorials

  6. Offline

    TopTobster5

    Use 'temp * 1.3 * input' instead of messing with brackets. Not sure what the best way to round it is.
     
  7. Offline

    ColonelHedgehog

    What you're doing will give you 130% of the item, however you could just use 1.3 * temp * input, since that's essentially what you're doing in the brackets (minus the "input" bit).

    To round it to the nearest tens, use:

    Code:
    BigDecimal bd = new BigDecimal(price).setScale(2, RoundingMode.HALF_EVEN);
    Then use BigDecimal#doubleValue().
     
  8. Offline

    Meatiex

    Awesome, Thanks :D
    Code:
    double price = Double.parseDouble(new BigDecimal(EcoData.getPrice(item) * 1.3 * input).setScale(2, RoundingMode.HALF_EVEN).toString());
     
  9. Offline

    ColonelHedgehog

    Eh, I'd use:

    Code:
    double price = new BigDecimal(EcoData.getPrice(item) * 1.3 * input).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
     
  10. Offline

    Meatiex

    will do, Thank you :D
     
Thread Status:
Not open for further replies.

Share This Page