Rounding Double to 1 Decimal place

Discussion in 'Plugin Development' started by TheGamesHawk2001, Dec 7, 2014.

Thread Status:
Not open for further replies.
  1. Hey guys, How would I round my Double to just 1 decimal e.g. from 1.552 to 1.5
    Thx :D
     
  2. Offline

    Creeoer

  3. Offline

    SuperOriginal

  4. Offline

    Creeoer

  5. Saw that and wasn't that sure on how to implement it into my plugin.

    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent event){
    3. Player p = event.getPlayer();
    4.  
    5. if(chatCool.containsKey(p.getName())){
    6. DecimalFormat df = new DecimalFormat("#.#");
    7. double chatTime = chatCool.get(p.getName());
    8. if(chatCool.get(p.getName()) > System.currentTimeMillis()){
    9. event.setCancelled(true);
    10. p.sendMessage(ChatColor.DARK_RED + "CANT TALK YET! talk in " + ((chatTime - System.currentTimeMillis())/1000) + " seconds!");
     
  6. Offline

    SuperOriginal

    There's not much implementing to do.. Create DecimalFormat, then use DecimalFormal#format()
     
  7. But in my instance, Where would I put it?, I have the "DecimalFormat decimalformat = new DecimalFormat("#.#");"
    above the "double chatTime = chatCool.get(p.getName());" Where would I actually put it?

    Here's all my code:

    Code:java
    1. static HashMap<String, Long> chatCool = new HashMap<String, Long>();
    2.  
    3. @EventHandler
    4. public void onChat(AsyncPlayerChatEvent event){
    5. Player p = event.getPlayer();
    6.  
    7. if(chatCool.containsKey(p.getName())){
    8. DecimalFormat decimalformat = new DecimalFormat("#.#");
    9. double chatTime = chatCool.get(p.getName());
    10. if(chatCool.get(p.getName()) > System.currentTimeMillis()){
    11. event.setCancelled(true);
    12. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "FAULT" + ChatColor.GRAY + ": You can not talk yet. You will be able to talk in " + ((chatTime - System.currentTimeMillis())/1000) + " seconds!");
    13. } else {
    14. addCooldown(p, 1); //You can change the 10 to whatever you want the cooldown to be
    15. }
    16.  
    17. } else {
    18. addCooldown(p, 1); //Same thing
    19. }
    20.  
    21. }
    22.  
    23. public void addCooldown(Player p, long secs){
    24. long currentMillis = System.currentTimeMillis();
    25. long coolTime = currentMillis + ((secs + 1) * 1000);
    26.  
    27. chatCool.put(p.getName(), coolTime);
    28. }
    29. }
     
  8. Offline

    SuperOriginal

    TheGamesHawk2001 That's fine.. It doesn't matter where you put it, as long as you have access to it.

    Now just wrap your math calculation in decimalformat.format()
     
  9. Offline

    au2001

    TheGamesHawk2001
    You would do that to replace line 12 :
    Code:java
    1. chatTime = Math.round((chatTime - System.currentTimeMillis()/1000) * 10) / 10;
    2. p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "FAULT" + ChatColor.GRAY + ": You can not talk yet. You will be able to talk in " + chatTime + " seconds!");


    BTW:
    If you round 1.552 to 1 decimal place, it doesn't make 1.5 but 1.6 :p
     
  10. Offline

    Totom3

    TheGamesHawk2001 SuperOriginal A simpler solution could be : Math.floor(x * 10) / 10. The way this works is simple: you "shift" each digit up (1.52 becomes 15.2) and then round it, which gets rid of the other decimals (15.2 becomes 15). Finally you shift back the digits to their original position by dividing by 10 and that's it.
     
  11. Offline

    SuperOriginal

    Totom3 Please enlighten me on how that is more simple.
     
  12. Offline

    au2001

    Totom3 I ninja'd you, but Math.round is better because if it is 0.9 it will round it to 1, not 0 :p
    And SuperOriginal, it is more efficient and faster to do it that way...
     
  13. Offline

    Rocoty

    Err...if all you do is to print the double value with one decimal you should use the String formatter:
    Code:
    String.format("My double is: %.1f", myDoubleValue);
     
  14. Offline

    mythbusterma

    au2001

    I'd like to see the benchmarks that support your idea. Even if that is true, the difference is marginal enough to not matter. In any circumstance. Whatsoever.

    In this case, readability triumphs, and the easiest answer to read is either SuperOriginal 's answer or Rocoty 's answer, both CLEARLY indicate exactly what they're trying to do without the need for comments, and do it in less lines.

    Edit: wouldn't cause a floating point error, due to dividing by ten only changing the base, still not a good idea though.
     
    Rocoty and SuperOriginal like this.
  15. Eh? No it won't. You're rounding to 1 decimal place - 0.9 is 1 decimal place, so it will just return 0.9... if it did return 1 (or 0 for that matter) then it'd be horribly wrong.
     
  16. Offline

    Jayke_

    Easy one liner!
    Code:java
    1.  
    2. double x = 12345.6789;
    3. x = Double.parseDouble(Double.toString(x).substring(0, Double.toString(x).indexOf('.') + 2));
    4. //x is now 12345.6
    5.  
     
    RFUDEO9EH likes this.
  17. Jayke_ I think we have enough solutions for this now, don't you? :p
     
  18. Offline

    Jayke_

    AdamQpzm There are never enough solutions to a problem.
     
    dxrknez likes this.
  19. Jayke_ I disagree - there'd be no real benefit to having hundreds of solutions posted to this little problem - there is a point at which someone is given too much choice, resulting in harder decisions. It's far easier to decide between 2 good things than 50 good things ;)
     
Thread Status:
Not open for further replies.

Share This Page