Java Code Examples for java.text.DecimalFormat#setParseBigDecimal()

The following examples show how to use java.text.DecimalFormat#setParseBigDecimal() . 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: CDecimalFormatter.java    From Open-Lowcode with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * creates a decimal formatter from the server message
 * 
 * @param reader reader of the message coming from the server
 * @throws OLcRemoteException if anything bad happens on the server
 * @throws IOException        if a problem happens during the message
 *                            transmission
 */
public CDecimalFormatter(MessageReader reader) throws OLcRemoteException, IOException {
	reader.returnNextStartStructure("DEF");
	minimum = reader.returnNextDecimalField("MIN");
	maximum = reader.returnNextDecimalField("MAX");
	linear = reader.returnNextBooleanField("LIN");
	colorscheme = reader.returnNextBooleanField("CLS");
	reader.returnNextEndStructure("DEF");
	String formatfordecimalstring = "###,###.###";
	formatfordecimal = new DecimalFormat(formatfordecimalstring);
	DecimalFormatSymbols formatforsymbol = formatfordecimal.getDecimalFormatSymbols();
	formatforsymbol.setGroupingSeparator(' ');
	formatforsymbol.setDecimalSeparator('.');
	formatfordecimal.setDecimalFormatSymbols(formatforsymbol);
	formatfordecimal.setParseBigDecimal(true);
}
 
Example 2
Source File: CurrencyStyleFormatter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 3
Source File: NanoWallet.java    From nano-wallet-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Return the currency formatted local currency amount as a string
 *
 * @return Formatted local currency amount
 */
public String getSendLocalCurrencyAmountFormatted() {
    if (sendLocalCurrencyAmount.length() > 0) {
        try {
            DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(getLocalCurrency().getLocale());
            df.setParseBigDecimal(true);
            BigDecimal bd = (BigDecimal) df.parseObject(sanitize(sendLocalCurrencyAmount));
            return currencyFormat(bd);
        } catch (ParseException e) {
            ExceptionHandler.handle(e);
        }
        return "";
    } else {
        return "";
    }
}
 
Example 4
Source File: CurrencyStyleFormatter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 5
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_parse_bigDecimal() throws Exception {
    // parseBigDecimal default to false
    DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
    assertFalse(form.isParseBigDecimal());
    form.setParseBigDecimal(true);
    assertTrue(form.isParseBigDecimal());

    Number result = form.parse("123.123");
    assertEquals(new BigDecimal("123.123"), result);

    form.setParseBigDecimal(false);
    assertFalse(form.isParseBigDecimal());

    result = form.parse("123.123");
    assertFalse(result instanceof BigDecimal);
}
 
Example 6
Source File: NumberStyleFormatter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public NumberFormat getNumberFormat(Locale locale) {
	NumberFormat format = NumberFormat.getInstance(locale);
	if (!(format instanceof DecimalFormat)) {
		if (this.pattern != null) {
			throw new IllegalStateException("Cannot support pattern for non-DecimalFormat: " + format);
		}
		return format;
	}
	DecimalFormat decimalFormat = (DecimalFormat) format;
	decimalFormat.setParseBigDecimal(true);
	if (this.pattern != null) {
		decimalFormat.applyPattern(this.pattern);
	}
	return decimalFormat;
}
 
Example 7
Source File: CurrencyStyleFormatter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected NumberFormat getNumberFormat(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
	format.setParseBigDecimal(true);
	format.setMaximumFractionDigits(this.fractionDigits);
	format.setMinimumFractionDigits(this.fractionDigits);
	if (this.roundingMode != null) {
		format.setRoundingMode(this.roundingMode);
	}
	if (this.currency != null) {
		format.setCurrency(this.currency);
	}
	if (this.pattern != null) {
		format.applyPattern(this.pattern);
	}
	return format;
}
 
Example 8
Source File: AbstractNumberProcessorBuilder.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 数値のフォーマッタを作成する。
 * <p>アノテーション{@link CsvNumberFormat}の値を元に作成します。</p>
 * @param field フィールド情報
 * @param config システム設定
 * @return アノテーション{@link CsvNumberFormat}が付与されていない場合は、空を返す。
 */
@SuppressWarnings("unchecked")
protected Optional<NumberFormatWrapper<N>> createFormatter(final FieldAccessor field, final Configuration config) {
    
    final Optional<CsvNumberFormat> formatAnno = field.getAnnotation(CsvNumberFormat.class);
    
    if(!formatAnno.isPresent()) {
        return Optional.empty();
    }
    
    final String pattern = formatAnno.get().pattern();
    if(pattern.isEmpty()) {
        return Optional.empty();
    }
    
    final boolean lenient = formatAnno.get().lenient();
    final Locale locale = Utils.getLocale(formatAnno.get().locale());
    final Optional<Currency> currency = formatAnno.get().currency().isEmpty() ? Optional.empty()
            : Optional.of(Currency.getInstance(formatAnno.get().currency()));
    final RoundingMode roundingMode = formatAnno.get().rounding();
    final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    
    final DecimalFormat formatter = new DecimalFormat(pattern, symbols);
    formatter.setParseBigDecimal(true);
    formatter.setRoundingMode(roundingMode);
    currency.ifPresent(c -> formatter.setCurrency(c));
    
    final NumberFormatWrapper<N> wrapper = new NumberFormatWrapper<>(formatter, (Class<N>)field.getType(), lenient);
    wrapper.setValidationMessage(formatAnno.get().message());
    
    return Optional.of(wrapper);
    
}
 
Example 9
Source File: DecimalFormatter.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public DecimalFormatter(String pattern) {
    final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    format = new DecimalFormat(pattern, decimalFormatSymbols);
    format.setParseIntegerOnly(false);
    format.setParseBigDecimal(false);
    format.setDecimalSeparatorAlwaysShown(true);
}
 
Example 10
Source File: LocaleParser.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static BigDecimal parseBigDecimal (@Nullable final String sStr,
                                          @Nonnull final DecimalFormat aNF,
                                          @Nullable final BigDecimal aDefault)
{
  ValueEnforcer.notNull (aNF, "NumberFormat");

  aNF.setParseBigDecimal (true);
  return (BigDecimal) parse (sStr, aNF, aDefault);
}
 
Example 11
Source File: LocaleSafeDecimalFormat.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Regardless of the default locale, comma ('.') is used as decimal separator
 *
 * @param source
 * @return
 * @throws ParseException
 */
public BigDecimal parse(String source) throws ParseException {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    DecimalFormat format = new DecimalFormat("#.#", symbols);
    format.setParseBigDecimal(true);
    return (BigDecimal) format.parse(source);
}
 
Example 12
Source File: StandardUtil.java    From Open-Lowcode with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Open Lowcode uses as default a number formatting that is non ambiguous for
 * both English speaking and French speaking countries. Decimal separator is dot
 * '.', thousands grouping separator is space ' '.
 * 
 * @return
 */
public static DecimalFormat getOLcDecimalFormatter() {
	String formatfordecimalstring = "###,###.###";
	DecimalFormat formatfordecimal = new DecimalFormat(formatfordecimalstring);
	DecimalFormatSymbols formatforsymbol = formatfordecimal.getDecimalFormatSymbols();
	formatforsymbol.setGroupingSeparator(' ');
	formatforsymbol.setDecimalSeparator('.');
	formatfordecimal.setDecimalFormatSymbols(formatforsymbol);
	formatfordecimal.setParseBigDecimal(true);
	return formatfordecimal;
}
 
Example 13
Source File: NumberFormatWrapperTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the formatter for the test using Combinations
 */
@Before
public void setUp() {
    DecimalFormat df1 = new DecimalFormat("#,##0.0##");
    this.formatter = new NumberFormatWrapper<>(df1, Integer.class);
    this.lenientFormatter = new NumberFormatWrapper<>(df1, Integer.class, true);
    
    DecimalFormat df2 = new DecimalFormat("#,##0.0##");
    df2.setParseBigDecimal(true);
    this.parseBigDecimalFormatter = new NumberFormatWrapper<>(df2, Integer.class);
    this.lenientParseBigDecimalFormatter = new NumberFormatWrapper<>(df2, Integer.class, true);
}
 
Example 14
Source File: NumberUtils.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * parse
 *
 * @param string
 * @param contentProperty
 * @return
 * @throws ParseException
 */
private static Number parse(String string, ExcelContentProperty contentProperty) throws ParseException {
    String format = contentProperty.getNumberFormatProperty().getFormat();
    RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
    DecimalFormat decimalFormat = new DecimalFormat(format);
    decimalFormat.setRoundingMode(roundingMode);
    decimalFormat.setParseBigDecimal(true);
    return decimalFormat.parse(string);
}
 
Example 15
Source File: BigDecimalConverter.java    From conf4j with MIT License 5 votes vote down vote up
@Override
protected Number parseWithFormat(String value, String format, String locale, Type type) {
    DecimalFormat formatter = getFormatter(format, locale);
    formatter.setParseBigDecimal(true);

    try {
        return formatter.parse(value);
    } catch (ParseException e) {
        throw new IllegalArgumentException(format("Unable to convert to BigDecimal. " +
                "The value doesn't match specified format: %s", format), e);
    }
}
 
Example 16
Source File: DataFormatter.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 
 * @param datatype
 *          should be one from {@link DataType}
 * @param mask
 * @param data
 * @return {@link String} - data which was formatted by mask or data if we have not correct mask
 */
public static String getFormatedString( DataType dataType, String mask, Object data ) {
  try {
    switch ( dataType ) {
      case NUMERIC:
        DecimalFormat decimalFormat = new DecimalFormat( mask );
        decimalFormat.setParseBigDecimal( true );
        return decimalFormat.format( data  );
      case DATE:
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat( mask );
        if ( data instanceof Date ) {
          return simpleDateFormat.format( data );
        }
        Date dateFromstring = DateDetector.getDateFromString( String.valueOf( data  ) );
        return simpleDateFormat.format( dateFromstring );
      case STRING:
      case UNKNOWN:
      case BOOLEAN:
      case BINARY:
      case IMAGE:
      case URL:
      default:
        return String.valueOf( data );
    }
  } catch ( Exception e ) {
    log.debug( DataFormatter.class.getName() + " could not apply mask to data. The original data was returned" ); //$NON-NLS-1$  //$NON-NLS-2$
    return String.valueOf( data );
  }
}
 
Example 17
Source File: HtmlField.java    From jspoon with MIT License 5 votes vote down vote up
private BigDecimal getBigDecimal(String value) {
    try {
        DecimalFormat decimalFormat = (spec.getFormat() == null)
                ? (DecimalFormat) DecimalFormat.getInstance(spec.getLocale())
                        : new DecimalFormat(spec.getFormat());

        decimalFormat.setParseBigDecimal(true);
        return (BigDecimal) decimalFormat.parse(value);
    }
    catch (ParseException e) {
        throw new BigDecimalParseException(value, spec.getFormat(), spec.getLocale());
    }

}
 
Example 18
Source File: BigDecimalParser.java    From poiji with MIT License 4 votes vote down vote up
private DecimalFormat getDecimalFormatInstance() {
    NumberFormat numberFormat = NumberFormat.getInstance();
    DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
    decimalFormat.setParseBigDecimal(true);
    return decimalFormat;
}
 
Example 19
Source File: DecimalFormatParser.java    From pentaho-reporting with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Sets the format for the filter. If the given format is no Decimal format, a ClassCastException is thrown
 *
 * @param format
 *          The format.
 * @throws NullPointerException
 *           if the given format is null
 * @throws ClassCastException
 *           if the format is no decimal format
 */
public void setFormatter( final Format format ) {
  if ( format == null ) {
    throw new NullPointerException( "The number format given must not be null." );
  }
  final DecimalFormat dfmt = (DecimalFormat) format;
  dfmt.setParseBigDecimal( true );
  super.setFormatter( dfmt );
}
 
Example 20
Source File: NumberUtils.java    From SaneEconomy with GNU General Public License v3.0 3 votes vote down vote up
private static DecimalFormat constructDecimalFormat() {
    DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();

    decimalFormat.setParseBigDecimal(true);

    return decimalFormat;
}