下面列出了java.text.DecimalFormat#setParseBigDecimal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
@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;
}
@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;
}
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);
}
@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;
}
/**
* 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 "";
}
}
@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;
}
/**
*
* @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 );
}
}
/**
* 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);
}
/**
* 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;
}
/**
* 数値のフォーマッタを作成する。
* <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);
}
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);
}
@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);
}
/**
* 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);
}
/**
* 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);
}
@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);
}
}
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());
}
}
private DecimalFormat getDecimalFormatInstance() {
NumberFormat numberFormat = NumberFormat.getInstance();
DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
decimalFormat.setParseBigDecimal(true);
return decimalFormat;
}
/**
* 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 );
}
private static DecimalFormat constructDecimalFormat() {
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();
decimalFormat.setParseBigDecimal(true);
return decimalFormat;
}