Double returning as zero?

Discussion in 'Plugin Development' started by Acer_Mortem, Feb 27, 2014.

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

    Acer_Mortem

    Code:
        public double blindPercent(int magnitude){
            double percent = 0;
            plugin.getLogger().info(magnitude + "---ggg");
            percent = (magnitude/15);
            plugin.getLogger().info(percent + "---");
            return percent;
        }
    In the above method, I'm trying to create a double, however for some reason it always returns as "0.0"

    Magnitude is not null, and infact equals 1 (as illustrated by the plugin.getLogger()).

    Why does this always return as 0.0?
     
  2. Offline

    Milkywayz

    Try changing magnitude to a double instead of an int, and cast double to the denominator.
     
  3. Acer_Mortem
    Double/Integer division will always use the lowest precision result, in this case you will just receive an integer result.

    You need to make sure all your variables are doubles or floats. This should fix your code.

    Code:
     public double blindPercent(int magnitude){
            double percent = 0;
            plugin.getLogger().info(magnitude + "---ggg");
            percent = ((double)magnitude/15D);
            plugin.getLogger().info(percent + "---");
            return percent;
        }
    I'd also recommend using a decimal formatter otherwise you will have a percentage with a lot of trailing numbers. Have a Google and give it a shot, it's a pretty useful class ;).
     
    AoH_Ruthless likes this.
Thread Status:
Not open for further replies.

Share This Page