Java Code Examples for java.text.DecimalFormatSymbols#getDecimalSeparator()

The following examples show how to use java.text.DecimalFormatSymbols#getDecimalSeparator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NumberUtils.java    From OpenEstate-IO with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: DurationValidation.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: DecimalStyle.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: DurationValidation.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 7
Source File: DecimalStyle.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: OdsUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static void updateDecimalSeparator( DecimalFormat numberFormat )
{
	DecimalFormatSymbols symbol = numberFormat.getDecimalFormatSymbols( );
	if ( symbol.getDecimalSeparator( ) != DECIMAL_SEPARATOR )
	{
		symbol.setDecimalSeparator( DECIMAL_SEPARATOR );
		numberFormat.setDecimalFormatSymbols( symbol );
	}
}
 
Example 9
Source File: DecimalStyle.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: DecimalStyle.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: DecimalStyle.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: GeneralUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: DecimalStyle.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: DecimalStyle.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: ConvertExcelToCSVProcessorTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 16
Source File: ConvertExcelToCSVProcessorTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 17
Source File: NumberUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example 18
Source File: NumberFormatter.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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 );
	}
}
 
Example 19
Source File: ParseBigDecimal.java    From super-csv with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: Helper.java    From smartcoins-wallet with MIT License 2 votes vote down vote up
public static char setDecimalSeparator(Locale locale) {

        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
        return decimalFormatSymbols.getDecimalSeparator();

    }