Skip to content

Commit 229afea

Browse files
committed
WW-3171 Uses proper number of digits when formatting double
1 parent 20eced9 commit 229afea

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

core/src/main/java/com/opensymphony/xwork2/conversion/impl/StringConverter.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
import java.lang.reflect.Array;
66
import java.lang.reflect.Member;
7+
import java.math.BigDecimal;
78
import java.text.DateFormat;
8-
import java.text.Format;
9-
import java.text.MessageFormat;
109
import java.text.NumberFormat;
1110
import java.text.SimpleDateFormat;
1211
import java.util.ArrayList;
@@ -62,11 +61,16 @@ public Object convertValue(Map<String, Object> context, Object target, Member me
6261
}
6362

6463
protected String convertToString(Locale locale, Object value) {
65-
if (value.getClass().isAssignableFrom(Number.class)) {
64+
if (Number.class.isInstance(value)) {
6665
NumberFormat format = NumberFormat.getNumberInstance(locale);
66+
format.setGroupingUsed(false);
67+
if (Double.class.isInstance(value) || BigDecimal.class.isInstance(value)) {
68+
format.setMinimumFractionDigits(1);
69+
}
6770
return format.format(value);
6871
} else {
6972
return String.valueOf(value);
7073
}
7174
}
75+
7276
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.opensymphony.xwork2.conversion.impl;
2+
3+
import com.opensymphony.xwork2.ActionContext;
4+
import org.apache.struts2.StrutsInternalTestCase;
5+
6+
import java.math.BigDecimal;
7+
import java.util.HashMap;
8+
import java.util.Locale;
9+
import java.util.Map;
10+
11+
public class StringConverterTest extends StrutsInternalTestCase {
12+
13+
public void testIntegerToStringConversionPL() throws Exception {
14+
// given
15+
StringConverter converter = new StringConverter();
16+
Map<String, Object> context = new HashMap<>();
17+
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
18+
19+
// when
20+
Object value = converter.convertValue(context, null, null, null, 234, null);
21+
22+
// then
23+
assertEquals("234", value);
24+
}
25+
26+
public void testDoubleToStringConversionPL() throws Exception {
27+
// given
28+
StringConverter converter = new StringConverter();
29+
Map<String, Object> context = new HashMap<>();
30+
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
31+
32+
// when
33+
Object value = converter.convertValue(context, null, null, null, 234.12, null);
34+
35+
// then
36+
assertEquals("234,12", value);
37+
}
38+
39+
public void testBigDecimalToStringConversionPL() throws Exception {
40+
// given
41+
StringConverter converter = new StringConverter();
42+
Map<String, Object> context = new HashMap<>();
43+
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
44+
45+
// when
46+
Object value = converter.convertValue(context, null, null, null, BigDecimal.valueOf(234.12), null);
47+
48+
// then
49+
assertEquals("234,12", value);
50+
}
51+
52+
}

core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeFieldValidatorTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void testRangeValidationWithError() throws Exception {
3636
//Explicitly set an out-of-range double for DoubleRangeValidatorTest
3737
Map<String, Object> context = new HashMap<>();
3838
HashMap<String, Object> params = new HashMap<>();
39-
params.put("percentage", 100.0123d);
39+
params.put("percentage", 100.12);
4040
context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
4141

4242
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, context);
@@ -50,8 +50,8 @@ public void testRangeValidationWithError() throws Exception {
5050
assertEquals(1, errorMessages.size());
5151

5252
String errorMessage = errorMessages.get(0);
53-
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
54-
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
53+
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
54+
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
5555
}
5656

5757
public void testRangeValidationNoError() throws Exception {
@@ -192,7 +192,7 @@ public void testRangeValidationWithExpressionsFail() throws Exception {
192192
//Explicitly set an out-of-range double for DoubleRangeValidatorTest
193193
Map<String, Object> context = new HashMap<>();
194194
HashMap<String, Object> params = new HashMap<>();
195-
params.put("percentage", 100.0123d);
195+
params.put("percentage", 100.12);
196196
context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
197197

198198
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, null, context);
@@ -205,8 +205,8 @@ public void testRangeValidationWithExpressionsFail() throws Exception {
205205
assertEquals(1, errorMessages.size());
206206

207207
String errorMessage = errorMessages.get(0);
208-
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
209-
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
208+
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
209+
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
210210
}
211211

212212
public void testExpressionParams() throws Exception {

core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-3.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<tr>
22
<td class="tdLabel"><label for="collection" class="label">mylabel:</label></td>
33
<td class="tdInput"><select name="collection" id="collection" title="mytitle" multiple="multiple" onmousedown="alert('onmousedown');" onmouseup="alert('onmouseup');" onmouseover="alert('onmouseover');" onmousemove="alert('onmousemove');" onmouseout="alert('onmouseout');">
4-
<option value="hello" selected="selected">1</option>
5-
<option value="foo" selected="selected">2</option>
4+
<option value="hello" selected="selected">1.0</option>
5+
<option value="foo" selected="selected">2.0</option>
66
<option value="&lt;cat&gt;">1.5</option>
77
</select>
88
<inputtype="hidden" id="__multiselect_collection" name="__multiselect_collection" value=""/>

0 commit comments

Comments
 (0)