mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3171 Uses proper number of digits when formatting double
This commit is contained in:
@@ -4,9 +4,8 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Member;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.Format;
|
||||
import java.text.MessageFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -62,11 +61,16 @@ public class StringConverter extends DefaultTypeConverter {
|
||||
}
|
||||
|
||||
protected String convertToString(Locale locale, Object value) {
|
||||
if (value.getClass().isAssignableFrom(Number.class)) {
|
||||
if (Number.class.isInstance(value)) {
|
||||
NumberFormat format = NumberFormat.getNumberInstance(locale);
|
||||
format.setGroupingUsed(false);
|
||||
if (Double.class.isInstance(value) || BigDecimal.class.isInstance(value)) {
|
||||
format.setMinimumFractionDigits(1);
|
||||
}
|
||||
return format.format(value);
|
||||
} else {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.opensymphony.xwork2.conversion.impl;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class StringConverterTest extends StrutsInternalTestCase {
|
||||
|
||||
public void testIntegerToStringConversionPL() throws Exception {
|
||||
// given
|
||||
StringConverter converter = new StringConverter();
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
|
||||
|
||||
// when
|
||||
Object value = converter.convertValue(context, null, null, null, 234, null);
|
||||
|
||||
// then
|
||||
assertEquals("234", value);
|
||||
}
|
||||
|
||||
public void testDoubleToStringConversionPL() throws Exception {
|
||||
// given
|
||||
StringConverter converter = new StringConverter();
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
|
||||
|
||||
// when
|
||||
Object value = converter.convertValue(context, null, null, null, 234.12, null);
|
||||
|
||||
// then
|
||||
assertEquals("234,12", value);
|
||||
}
|
||||
|
||||
public void testBigDecimalToStringConversionPL() throws Exception {
|
||||
// given
|
||||
StringConverter converter = new StringConverter();
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
context.put(ActionContext.LOCALE, new Locale("pl", "PL"));
|
||||
|
||||
// when
|
||||
Object value = converter.convertValue(context, null, null, null, BigDecimal.valueOf(234.12), null);
|
||||
|
||||
// then
|
||||
assertEquals("234,12", value);
|
||||
}
|
||||
|
||||
}
|
||||
+6
-6
@@ -36,7 +36,7 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase {
|
||||
//Explicitly set an out-of-range double for DoubleRangeValidatorTest
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
params.put("percentage", 100.0123d);
|
||||
params.put("percentage", 100.12);
|
||||
context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
|
||||
|
||||
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, context);
|
||||
@@ -50,8 +50,8 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase {
|
||||
assertEquals(1, errorMessages.size());
|
||||
|
||||
String errorMessage = errorMessages.get(0);
|
||||
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
|
||||
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
|
||||
}
|
||||
|
||||
public void testRangeValidationNoError() throws Exception {
|
||||
@@ -192,7 +192,7 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase {
|
||||
//Explicitly set an out-of-range double for DoubleRangeValidatorTest
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
params.put("percentage", 100.0123d);
|
||||
params.put("percentage", 100.12);
|
||||
context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
|
||||
|
||||
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.EXPRESSION_VALIDATION_ACTION, null, context);
|
||||
@@ -205,8 +205,8 @@ public class DoubleRangeFieldValidatorTest extends XWorkTestCase {
|
||||
assertEquals(1, errorMessages.size());
|
||||
|
||||
String errorMessage = errorMessages.get(0);
|
||||
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.0123.", errorMessage);
|
||||
assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
|
||||
assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
|
||||
}
|
||||
|
||||
public void testExpressionParams() throws Exception {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<tr>
|
||||
<td class="tdLabel"><label for="collection" class="label">mylabel:</label></td>
|
||||
<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');">
|
||||
<option value="hello" selected="selected">1</option>
|
||||
<option value="foo" selected="selected">2</option>
|
||||
<option value="hello" selected="selected">1.0</option>
|
||||
<option value="foo" selected="selected">2.0</option>
|
||||
<option value="<cat>">1.5</option>
|
||||
</select>
|
||||
<inputtype="hidden" id="__multiselect_collection" name="__multiselect_collection" value=""/>
|
||||
|
||||
Reference in New Issue
Block a user