DecimalFormat problem

Discussion in 'Plugin Development' started by Kassestral, Jan 6, 2015.

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

    Kassestral

    I am trying to put the users balance to a two decimal double? But for some reason it keeps returning with one decimal place? any help?

    Code:
    package com.kassestral.plugins.currency;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.UUID;
    
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    public class Account {
    
        Plugin plugin = Plugin.plugin;
       
        private double balance;
        private YamlConfiguration account;
        private File account_file;
        private DecimalFormat decimal = new DecimalFormat("0.00");
       
       
        public Account(Player player) {
            UUID UUID = player.getUniqueId();
            this.account_file = new File(plugin.getDataFolder() + File.separator + "accounts" + File.separator + UUID.toString() + ".yml");
            this.account = YamlConfiguration.loadConfiguration(account_file);
            this.balance = Double.valueOf(decimal.format(account.getDouble("balance")));
        }
    
    
        public double getBalance() {
            return this.balance;
        }
    
    
        public void setBalance(double amount) throws IOException {
            this.balance = Double.valueOf(decimal.format(amount));
            account.set("balance", this.balance);
            account.save(account_file);
        }
       
        public void addBalance(double amount) throws IOException {
            this.balance = this.balance + Double.valueOf(decimal.format(amount));
            account.set("balance", this.balance + Double.valueOf(decimal.format(amount)));
            account.save(account_file);
        }
       
        public void deductBalance(double amount) throws IOException {
            this.balance = this.balance - Double.valueOf(decimal.format(amount));
            account.set("balance", this.balance - Double.valueOf(decimal.format(amount)));
            account.save(account_file);
        }
       
        public boolean hasBalance(double amount) {
            if(this.balance >= amount) {
                return true;
            }
            else {
                return false;
            }
        }
       
    }
     
  2. Offline

    Skionz

    @Kassestral Are you sure your exporting it correctly? It looks fine to me.
     
  3. Offline

    Kassestral

    @Skionz I completely deleted the plugin and it's folder from the plugin's folder, I then re-exported it and still it doesn't work
     

    Attached Files:

  4. Offline

    Europia79

    @Kassestral

    Try String.format() :

    Code:java
    1. package com.kassestral.plugins.currency;
    2.  
    3. import java.io.File;
    4. import java.util.UUID;
    5.  
    6. import org.bukkit.configuration.file.YamlConfiguration;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class AccountFactory {
    10.  
    11. Plugin plugin;
    12.  
    13. public AccountFactory(Plugin reference) {
    14. this.plugin = reference;
    15. }
    16.  
    17. public Account loadOrCreate(Player player) {
    18. UUID id = player.getUniqueId();
    19. File accountFile = new File(plugin.getDatafolder() + File.separator + "accounts" + File.separator + UUID.toString() + ".yml");
    20. YamlConfiguration accountYml = YamlConfiguration.loadConfiguration(accountFile);
    21. double balance = accountYml.getDouble("balance", 0.0);
    22. return new Account(id, balance, accountFile, accountYml);
    23. }
    24.  
    25. }


    Code:java
    1. package com.kassestral.plugins.currency;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8.  
    9. public class Account {
    10.  
    11. private UUID id;
    12. private Balance balance;
    13. private File file;
    14. private YamlConfiguration config;
    15.  
    16. public Account(UUID id, double amount, File file, YamlConfiguration config) {
    17. this.id = id;
    18. this.balance = amount;
    19. this.file = file;
    20. this.config = config;
    21. }
    22.  
    23. public UUID getId() {
    24. return this.id;
    25. }
    26.  
    27. public double getBalance() {
    28. return this.balance;
    29. }
    30.  
    31. public String formatBalance() {
    32. return String.format("%.2f", balance);
    33. }
    34.  
    35. public Account setBalance(double amount) {
    36. this.balance = amount;
    37. }
    38.  
    39. public Account addBalance(double amount) {
    40. this.balance = balance + amount;
    41. }
    42.  
    43. public Account deductBalance(double amount) {
    44. this.balance = balance - amount;
    45. }
    46.  
    47. public boolean hasBalance(double amount) {
    48. return balance >= amount;
    49. }
    50.  
    51. public boolean save() {
    52. config.set("balance", formatBalance());
    53. try {
    54. config.save(file);
    55. } catch (IOException ex) {
    56. ex.printStackTrace();
    57. return false;
    58. }
    59. return true;
    60. }
    61.  
    62. }
     
    1Rogue likes this.
Thread Status:
Not open for further replies.

Share This Page