Percent's in configuration file?

Discussion in 'Plugin Development' started by RemotelyOperated, May 23, 2013.

Thread Status:
Not open for further replies.
  1. Okay so this has been something I have wondered about and its a challenge for all of you because I have found no documentation about this subject. Percents. I have tried on my own to make a percentage system out of a hundred using Util.Random class in java. So an example on how to use percentages would be honestly the greatest thing you could do for me at this moment. If I am totally wrong and you can find me documentation please leave a link.
    Thanks for your time ;)
     
  2. Offline

    CubixCoders

    You could store the percent as it's decimal form, then when you get it out just multiply it by 100.
     
  3. You are a genius. I'll try that and post the code back here if anyone wants it for later :3
     
  4. Offline

    CubixCoders

    Thanks, i saw no one was helping you so i thought i'd give it a shot
     
    RemotelyOperated likes this.
  5. Offline

    AstramG

    Or you could just store the number of the percentage without it being in decimal form, but without the percent sign. Therefore you don't need the extra computation of *100. I mean that you can do:
    if (NUMBERINCONFIG <= 100)
    Instead of
    if (NUMBERINCONFIG*100 <= 100)
    Just making it less confusing for "noobs" who are using your plugin and don't know how to convert percents in to decimals and vice versa.
     
  6. Offline

    Deathmarine

    You could store your percentage as a double.
    Code:
    Percentage: 98.5
    Retrieve it from your configuration.
    Code:java
    1. double percent = plugin.getConfig().getDouble("Percentage");

    Considering Random.class generates doubles between 0 and 1 you can use Random's nextDouble() and times that by the max percentage(100).
    Example:

    Code:java
    1. Random gen = new Random();
    2.  
    3. public boolean percentage(){
    4. double percent = plugin.getConfig().getDouble("Percentage", 0D);
    5. return 100 * gen.nextDouble() < percent;
    6. }


    The generated product would roughly read as "if a number generated out of a hundred is the less than the percentage it will show true".

    This will only get you close to a random measurement not an exact. ~1+/- Here is an example to test with:
    Code:java
    1. public static void main(String[] args){
    2. System.out.println("Check a \"double\" percentage based on this code.\n>");
    3. Scanner scan = new Scanner(System.in);
    4. Random gen = new Random();
    5. String s = scan.nextLine();
    6. try{
    7. double percent = Double.parseDouble(s);
    8. int check = 0;
    9. for(int i = 0;i < 100;i++){
    10. double range = 100 * gen.nextDouble();
    11. if(range<percent)
    12. check++;
    13. }
    14. System.out.println("Using " + s + " out of a 100: " + check + " was the calculation.");
    15. }catch (NumberFormatException nfe){
    16. System.out.println(s + " is not a number.");
    17. }
    18. scan.close();
    19. System.exit(0);
    20. }
     
    Zarius likes this.
  7. Offline

    AstramG

    Deathmarine Good idea but possibly a little too advanced for the person in the op
     
Thread Status:
Not open for further replies.

Share This Page