下面列出了java.text.DecimalFormatSymbols#getDecimalSeparator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testParseDoubleNonRootLocale() throws Exception {
final DecimalFormatSymbols fr_FR = DecimalFormatSymbols.getInstance(new Locale("fr","FR"));
final char groupChar = fr_FR.getGroupingSeparator();
final char decimalChar = fr_FR.getDecimalSeparator();
double value = 10898.83491D;
String doubleString1 = "10898"+decimalChar+"83491";
String doubleString2 = "10"+groupChar+"898"+decimalChar+"83491";
IndexSchema schema = h.getCore().getLatestSchema();
assertNotNull(schema.getFieldOrNull("double_d")); // should match dynamic field "*_d"
assertNull(schema.getFieldOrNull("not_in_schema"));
SolrInputDocument d = processAdd("parse-double-french-no-run-processor",
doc(f("id", "140"), f("double_d", doubleString1),
f("not_in_schema", doubleString2)));
assertNotNull(d);
assertThat(d.getFieldValue("double_d"), IS_DOUBLE);
assertEquals(value, (Double)d.getFieldValue("double_d"), EPSILON);
assertThat(d.getFieldValue("not_in_schema"), IS_DOUBLE);
assertEquals(value, (Double)d.getFieldValue("not_in_schema"), EPSILON);
}
/**
* Test, if a string contains a parsable number.
*
* @param value the value to check
* @param locale the locale, against which the value is checked
* (checks locale specific decimal and grouping separators)
* @return true, if the provided value contains of numbers
*/
public static boolean isNumeric(String value, Locale locale) {
if (value == null) return false;
int start = 0;
final DecimalFormatSymbols symbols = (locale != null) ?
DecimalFormatSymbols.getInstance(locale) :
DecimalFormatSymbols.getInstance();
if (value.startsWith("+") || value.startsWith("-")) start++;
boolean fraction = false;
for (int i = start; i < value.length(); i++) {
final char c = value.charAt(i);
if (c == symbols.getDecimalSeparator() && !fraction) {
fraction = true;
continue;
}
if (c == symbols.getGroupingSeparator() && !fraction) {
continue;
}
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
/**
* Validates that the given value contains only digits, but not e.g. a
* character 'd'. Java would interpret the input 3d as double value 3.0.
* Anyway, this must not succeed.
*
* @param valueToCheck
* The value to be checked.
* @param component
* The current component.
* @return <code>true</code> if the value is valid.
*/
private static boolean validateOnlyDigits(String valueToCheck,
FacesContext facesContext) {
Locale locale = LocaleHandler.getLocaleFromString(BaseBean
.getUserFromSession(facesContext).getLocale());
DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
boolean decSepFound = false;
for (char c : valueToCheck.toCharArray()) {
if (!decSepFound && c == dfs.getDecimalSeparator()) {
decSepFound = true;
continue;
}
if (c == dfs.getGroupingSeparator()) {
continue;
}
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
public void testParseFloatNonRootLocale() throws Exception {
final DecimalFormatSymbols fr_FR = DecimalFormatSymbols.getInstance(new Locale("fr","FR"));
final char groupChar = fr_FR.getGroupingSeparator();
final char decimalChar = fr_FR.getDecimalSeparator();
float value = 10898.83491F;
String floatString1 = "10898"+decimalChar+"83491";
String floatString2 = "10"+groupChar+"898"+decimalChar+"83491";
IndexSchema schema = h.getCore().getLatestSchema();
assertNotNull(schema.getFieldOrNull("float_f")); // should match dynamic field "*_f"
assertNull(schema.getFieldOrNull("not_in_schema"));
SolrInputDocument d = processAdd("parse-float-french-no-run-processor",
doc(f("id", "140"), f("float_f", floatString1),
f("not_in_schema", floatString2)));
assertNotNull(d);
assertThat(d.getFieldValue("float_f"), IS_FLOAT);
assertEquals(value, (Float)d.getFieldValue("float_f"), EPSILON);
assertThat(d.getFieldValue("not_in_schema"), IS_FLOAT);
assertEquals(value, (Float)d.getFieldValue("not_in_schema"), EPSILON);
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
@Test
public void testCustomValueSeparatorWithEL() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("csv.delimiter", "|");
testRunner.enqueue(new File("src/test/resources/dataformatting.xlsx").toPath(), attributes);
testRunner.setProperty(CSVUtils.VALUE_SEPARATOR, "${csv.delimiter}");
testRunner.setProperty(ConvertExcelToCSVProcessor.FORMAT_VALUES, "true");
testRunner.run();
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.SUCCESS, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.ORIGINAL, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.FAILURE, 0);
MockFlowFile ff = testRunner.getFlowFilesForRelationship(ConvertExcelToCSVProcessor.SUCCESS).get(0);
Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM));
assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue();
String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator))
? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator)
? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
ff.assertContentEquals(String.format("Numbers|Timestamps|Money\n" +
"1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\n" +
"1234%1$s46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123%1$s45\n" +
"1234%1$s5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123%1$s45\n" +
"1%2$s234%1$s46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1%2$s023%1$s45\n" +
"1%2$s234%1$s4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1%2$s023%1$s45\n" +
"9%1$s88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08||\n" +
"9%1$s8765E+08||\n", decimalSeparator, groupingSeparator));
}
private static void updateDecimalSeparator( DecimalFormat numberFormat )
{
DecimalFormatSymbols symbol = numberFormat.getDecimalFormatSymbols( );
if ( symbol.getDecimalSeparator( ) != DECIMAL_SEPARATOR )
{
symbol.setDecimalSeparator( DECIMAL_SEPARATOR );
numberFormat.setDecimalFormatSymbols( symbol );
}
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
/**
* Validates that the number of decimal places is not exceeding the amount
* given in {@link DEC_PLACES_BOUND}.
*
* @param valueToCheck
* The value to be checked.
* @param component
* The current component.
* @return <code>true</code> if the value is valid.
*/
private static boolean validatePrecision(String valueToCheck,
FacesContext facesContext) {
Locale locale = LocaleHandler.getLocaleFromString(BaseBean
.getUserFromSession(facesContext).getLocale());
DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
char decimalSeparator = dfs.getDecimalSeparator();
int pos = valueToCheck.lastIndexOf(decimalSeparator);
if (pos != -1 && valueToCheck.length() - 1 > pos + DEC_PLACES_BOUND) {
return false;
} else {
return true;
}
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
public static char getDecimalSeparator(Locale locale) {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
DecimalFormatSymbols decimalFormatSymbols = df.getDecimalFormatSymbols();
logger.debug("IN");
char decimals = decimalFormatSymbols.getDecimalSeparator();
logger.debug("OUT");
return decimals;
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
private static DecimalStyle create(Locale locale) {
DecimalFormatSymbols oldSymbols = DecimalFormatSymbols.getInstance(locale);
char zeroDigit = oldSymbols.getZeroDigit();
char positiveSign = '+';
char negativeSign = oldSymbols.getMinusSign();
char decimalSeparator = oldSymbols.getDecimalSeparator();
if (zeroDigit == '0' && negativeSign == '-' && decimalSeparator == '.') {
return STANDARD;
}
return new DecimalStyle(zeroDigit, positiveSign, negativeSign, decimalSeparator);
}
@Test
public void testQuoting() throws Exception {
testRunner.enqueue(new File("src/test/resources/dataformatting.xlsx").toPath());
testRunner.setProperty(CSVUtils.QUOTE_MODE, CSVUtils.QUOTE_MINIMAL);
testRunner.setProperty(ConvertExcelToCSVProcessor.FORMAT_VALUES, "true");
testRunner.run();
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.SUCCESS, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.ORIGINAL, 1);
testRunner.assertTransferCount(ConvertExcelToCSVProcessor.FAILURE, 0);
MockFlowFile ff = testRunner.getFlowFilesForRelationship(ConvertExcelToCSVProcessor.SUCCESS).get(0);
Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM));
assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
char decimalSeparator = decimalFormatSymbols.getDecimalSeparator();
char groupingSeparator = decimalFormatSymbols.getGroupingSeparator();
ff.assertContentEquals("Numbers,Timestamps,Money\n" +
addQuotingIfNeeded(String.format("1234%1$s456", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "," +
addQuotingIfNeeded(String.format("$ 123%1$s45", decimalSeparator)) + "\n" +
addQuotingIfNeeded(String.format("1234%1$s46", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "," +
addQuotingIfNeeded(String.format("£ 123%1$s45", decimalSeparator)) + "\n" +
addQuotingIfNeeded(String.format("1234%1$s5", decimalSeparator)) + ",\"" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "\"," +
addQuotingIfNeeded(String.format("¥ 123%1$s45", decimalSeparator)) + "\n" +
addQuotingIfNeeded(String.format("1%2$s234%1$s46", decimalSeparator, groupingSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "," +
addQuotingIfNeeded(String.format("$ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("1%2$s234%1$s4560", decimalSeparator, groupingSeparator)) + "," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "," +
addQuotingIfNeeded(String.format("£ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("9%1$s88E+08", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "," +
addQuotingIfNeeded(String.format("¥ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("9%1$s877E+08", decimalSeparator)) + ",,\n" +
addQuotingIfNeeded(String.format("9%1$s8765E+08", decimalSeparator)) + ",,\n");
}
/**
* @param origin number that is needed to validate
* @param locale specific geographic location to validate format on origin param
* @return true if number format is valid for that locale
*/
public static boolean isValidLocaleDouble(final String origin, Locale locale) {
final DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(locale);
final DecimalFormatSymbols fs = df.getDecimalFormatSymbols();
final String doublePattern =
"\\d+\\" + fs.getGroupingSeparator()
+ "\\d\\d\\d\\" + fs.getDecimalSeparator()
+ "\\d+|\\d+\\" + fs.getDecimalSeparator()
+ "\\d+|\\d+\\" + fs.getGroupingSeparator()
+ "\\d\\d\\d|\\d+";
return origin.matches(doublePattern);
}
/**
* initializes numeric format pattern
*
* @param patternStr
* ths string used for formatting numeric data
*/
public void applyPattern( String patternStr )
{
try
{
patternStr = processPatternAttributes( patternStr );
this.formatPattern = patternStr;
hexFlag = false;
roundPrecision = -1;
realPattern = formatPattern;
// null format String
if ( this.formatPattern == null )
{
numberFormat = NumberFormat.getInstance( locale.toLocale( ) );
numberFormat.setGroupingUsed( false );
DecimalFormatSymbols symbols = new DecimalFormatSymbols( locale
.toLocale( ) );
decimalSeparator = symbols.getDecimalSeparator( );
decimalFormat = new DecimalFormat( "", //$NON-NLS-1$
new DecimalFormatSymbols( locale.toLocale( ) ) );
decimalFormat.setMinimumIntegerDigits( 1 );
decimalFormat.setGroupingUsed( false );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
return;
}
// Single character format string
if ( patternStr.length( ) == 1 )
{
handleSingleCharFormatString( patternStr.charAt( 0 ) );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
return;
}
// Named formats and arbitrary format string
handleNamedFormats( patternStr );
roundPrecision = getRoundPrecision( numberFormat );
applyPatternAttributes( );
}
catch ( Exception illeagueE )
{
logger.log( Level.WARNING, illeagueE.getMessage( ), illeagueE );
}
}
public static char setDecimalSeparator(Locale locale) {
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
return decimalFormatSymbols.getDecimalSeparator();
}
/**
* Fixes the symbols in the input String (currently only decimal separator and grouping separator) so that the
* String can be parsed as a BigDecimal.
*
* @param s
* the String to fix
* @param symbols
* the decimal format symbols
* @return the fixed String
*/
private static String fixSymbols(final String s, final DecimalFormatSymbols symbols) {
final char groupingSeparator = symbols.getGroupingSeparator();
final char decimalSeparator = symbols.getDecimalSeparator();
return s.replace(String.valueOf(groupingSeparator), "").replace(decimalSeparator, DEFAULT_DECIMAL_SEPARATOR);
}