Trim stored precision to below that of a double, to prevent rounding issues.

This commit is contained in:
KHobbits 2013-05-05 00:58:44 +01:00
parent aeb1b4601c
commit 801acbb004
2 changed files with 55 additions and 47 deletions

View file

@ -89,12 +89,12 @@ public abstract class UserData extends PlayerExtension implements IConf
public void setMoney(double value) public void setMoney(double value)
{ {
money = value; money = Util.sanitizeMoney(value);
if (Math.abs(money) > ess.getSettings().getMaxMoney()) if (Math.abs(money) > ess.getSettings().getMaxMoney())
{ {
money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney(); money = money < 0 ? -ess.getSettings().getMaxMoney() : ess.getSettings().getMaxMoney();
} }
config.setProperty("money", value); config.setProperty("money", money);
config.save(); config.save();
} }
private Map<String, Object> homes; private Map<String, Object> homes;
@ -318,7 +318,6 @@ public abstract class UserData extends PlayerExtension implements IConf
config.setProperty("lastlocation", loc); config.setProperty("lastlocation", loc);
config.save(); config.save();
} }
private Location logoutLocation; private Location logoutLocation;
private Location _getLogoutLocation() private Location _getLogoutLocation()
@ -348,7 +347,6 @@ public abstract class UserData extends PlayerExtension implements IConf
config.setProperty("logoutlocation", loc); config.setProperty("logoutlocation", loc);
config.save(); config.save();
} }
private long lastTeleportTimestamp; private long lastTeleportTimestamp;
private long _getLastTeleportTimestamp() private long _getLastTeleportTimestamp()
@ -867,31 +865,31 @@ public abstract class UserData extends PlayerExtension implements IConf
} }
public void setConfigProperty(String node, Object object) public void setConfigProperty(String node, Object object)
{ {
final String prefix = "info."; final String prefix = "info.";
node = prefix+node; node = prefix + node;
if (object instanceof Map) if (object instanceof Map)
{ {
config.setProperty(node, (Map) object); config.setProperty(node, (Map)object);
} }
else if (object instanceof List) else if (object instanceof List)
{ {
config.setProperty(node, (List<String>) object); config.setProperty(node, (List<String>)object);
} }
else if (object instanceof Location) else if (object instanceof Location)
{ {
config.setProperty(node, (Location) object); config.setProperty(node, (Location)object);
} }
else if (object instanceof ItemStack) else if (object instanceof ItemStack)
{ {
config.setProperty(node, (ItemStack) object); config.setProperty(node, (ItemStack)object);
} }
else else
{ {
config.setProperty(node, object); config.setProperty(node, object);
} }
config.save(); config.save();
} }
public Set<String> getConfigKeys() public Set<String> getConfigKeys()
{ {
@ -913,9 +911,9 @@ public abstract class UserData extends PlayerExtension implements IConf
public Map<String, Object> getConfigMap(String node) public Map<String, Object> getConfigMap(String node)
{ {
if (config.isConfigurationSection("info."+node)) if (config.isConfigurationSection("info." + node))
{ {
return config.getConfigurationSection("info."+node).getValues(true); return config.getConfigurationSection("info." + node).getValues(true);
} }
return new HashMap<String, Object>(); return new HashMap<String, Object>();
} }

View file

@ -1,6 +1,8 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._; import static com.earth2me.essentials.I18n._;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
@ -521,12 +523,19 @@ public class Util
} }
return is; return is;
} }
public static double sanitizeMoney(final double value)
{
BigDecimal money = new BigDecimal(value, MathContext.DECIMAL128);
return money.doubleValue();
}
private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
public static String formatAsCurrency(final double value) public static String formatAsCurrency(final double value)
{ {
double fvalue = sanitizeMoney(value);
dFormat.setRoundingMode(RoundingMode.FLOOR); dFormat.setRoundingMode(RoundingMode.FLOOR);
String str = dFormat.format(value); String str = dFormat.format(fvalue);
if (str.endsWith(".00")) if (str.endsWith(".00"))
{ {
str = str.substring(0, str.length() - 3); str = str.substring(0, str.length() - 3);
@ -714,10 +723,11 @@ public class Util
return pattern.matcher(input).replaceAll("\u00a7$1"); return pattern.matcher(input).replaceAll("\u00a7$1");
} }
private static final Pattern IPPATTERN = Pattern.compile( private static final Pattern IPPATTERN = Pattern.compile(
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
public static boolean validIP(String ipAddress) { public static boolean validIP(String ipAddress)
{
return IPPATTERN.matcher(ipAddress).matches(); return IPPATTERN.matcher(ipAddress).matches();
} }
} }