Skip to content

Commit

Permalink
Parse floating point values using current default locale
Browse files Browse the repository at this point in the history
Fixes #55 : Locales that use a decimal separator other than dot "."
would throw NumberFormatException when parsing the content of the Estimated
field.

Detected three other places in the code that would also parse
potentially locale-formatted strings with the universal .valueOf()
parsing methods that expect dots.

Changed them to use an instance of NumberFormat instead.
  • Loading branch information
Adrian Wilkins committed Feb 6, 2015
1 parent 2dd885d commit f59f243
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.sf.redmine_mylyn.internal.ui.editor;

import java.text.NumberFormat;
import java.text.ParseException;

import net.sf.redmine_mylyn.core.RedmineCorePlugin;
import net.sf.redmine_mylyn.internal.ui.Images;
import net.sf.redmine_mylyn.internal.ui.Messages;
Expand Down Expand Up @@ -120,9 +123,20 @@ private int getValue() {

private void setValue(String val) {
if(!val.equals(getTaskAttribute().getValue())) {
if(Float.valueOf(val) == 0) {

NumberFormat nf = NumberFormat.getInstance();
float value = 0;

try {
value = nf.parse(val).floatValue();
} catch (ParseException parsex) {
// do nothing
}

if(value == 0) {
val = "";
}

getTaskAttribute().setValue(val);
attributeChanged();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.sf.redmine_mylyn.internal.ui.editor;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -78,8 +80,9 @@ protected void validateEstimatedHours(TaskData taskData, ErrorMessageCollector c
if (attribute != null) {
if (!attribute.getValue().trim().isEmpty()) {
try {
Double.valueOf(attribute.getValue().trim());
} catch (NumberFormatException e) {
NumberFormat nf = NumberFormat.getInstance();
nf.parse(attribute.getValue().trim()).doubleValue();
} catch (ParseException e) {
collector.add(attribute, RedmineAttribute.ESTIMATED.getLabel() + Messages.ERRMSG_FLOAT);
}
}
Expand All @@ -91,8 +94,9 @@ protected void validateTimeEntry(TaskData taskData, ErrorMessageCollector collec
if (attribute != null) {
if (!attribute.getValue().trim().isEmpty()) {
try {
Double.valueOf(attribute.getValue().trim());
} catch (NumberFormatException e) {
NumberFormat nf = NumberFormat.getInstance();
nf.parse(attribute.getValue().trim());
} catch (ParseException e) {
collector.add(attribute, RedmineAttribute.TIME_ENTRY_HOURS.getLabel() + Messages.ERRMSG_FLOAT);
}

Expand Down Expand Up @@ -169,13 +173,18 @@ protected void validateCustomAttribute(TaskAttribute taskAttribute, CustomField
protected boolean validateCustomAttributeType(String value, CustomField customField) {
try {
switch (customField.getFieldFormat()) {
case FLOAT: Double.valueOf(value); break;
case FLOAT:
NumberFormat nf = NumberFormat.getInstance();
nf.parse(value);
case INT: Integer.valueOf(value); break;
default: return true;
}
} catch (NumberFormatException e) {
return false;
} catch (ParseException e) {
return false;
}

return true;
}

Expand Down

0 comments on commit f59f243

Please sign in to comment.