/** * Get the decimal separator character, if available * @return the decimal separator character, or empty if the number type is not a decimal number */ @Override public Optional<Character> getDecimalSymbol() { if (!TypeUtils.isDecimalNumber(getNumberType())) { return Optional.empty(); } return getDecimalFormatSymbols().map(dfs -> dfs.getDecimalSeparator()); }
/** * Gets the NumberFormat to use to convert values * @param locale Locale to use * @return the numberFormat If a NumberFormat was specified using {@link #setNumberFormat(NumberFormat)}, this one * is returned. Otherwise, a NumberFormat is obtained using given Locale */ public NumberFormat getNumberFormat(Locale locale) { Locale lcl = (locale != null) ? locale : LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); return (numberFormat != null) ? numberFormat : TypeUtils.isDecimalNumber(numberType) ? NumberFormat.getNumberInstance(lcl) : NumberFormat.getIntegerInstance(lcl); }
/** * Build a {@link NumberFormat} using default {@link Locale} * @param value Value to present * @param disableGrouping <code>true</code> to disable groups separator * @param hideZeroDecimals <code>true</code> to hide decimals when all zero * @return NumberFormat */ private static NumberFormat getNumberFormatForDefaultLocale(Number value, boolean disableGrouping, boolean hideZeroDecimals) { NumberFormat numberFormat; if (value != null && TypeUtils.isDecimalNumber(value.getClass())) { numberFormat = NumberFormat.getInstance(); if (hideZeroDecimals && !FormatUtils.hasDecimals(value.doubleValue())) { numberFormat.setMaximumFractionDigits(0); } } else { numberFormat = NumberFormat.getIntegerInstance(); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return numberFormat; }
/** * Gets the NumberFormat to use to convert values * @param locale Locale to use * @return the numberFormat If a NumberFormat was specified using {@link #setNumberFormat(NumberFormat)}, this one * is returned. Otherwise, a NumberFormat is obtained using given Locale */ public NumberFormat getNumberFormat(Locale locale) { Locale lcl = (locale != null) ? locale : LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); return (numberFormat != null) ? numberFormat : TypeUtils.isDecimalNumber(numberType) ? NumberFormat.getNumberInstance(lcl) : NumberFormat.getIntegerInstance(lcl); }
@Override public void validate(T v) throws ValidationException { if (v != null) { String string = null; if (TypeUtils.isDecimalNumber(v.getClass())) { BigDecimal bd = (v instanceof BigDecimal) ? (BigDecimal) v : BigDecimal.valueOf(v.doubleValue()); string = bd.stripTrailingZeros().toPlainString(); } else { BigInteger bi = (v instanceof BigInteger) ? (BigInteger) v : BigInteger.valueOf(v.longValue()); string = bi.toString(); } if (string != null) { if (string.startsWith("-")) { string = string.substring(1); } int index = string.indexOf("."); int itg = index < 0 ? string.length() : index; int fct = index < 0 ? 0 : string.length() - index - 1; if (itg > integral) { throw new ValidationException(message, messageCode); } if (fct > fractional) { throw new ValidationException(message, messageCode); } } } }
if (TypeUtils.isDecimalNumber(v.getClass())) { if (((Number) v).doubleValue() > max) { throw new ValidationException(message, messageCode, max);
if (TypeUtils.isDecimalNumber(v.getClass())) { if (((Number) v).doubleValue() < min) { throw new ValidationException(message, messageCode, min);
if (TypeUtils.isDecimalNumber(value.getClass())) { return FormDataFormats.DECIMAL_FORMAT.format(value);
@Override public NumberFormat getNumberFormat(Class<? extends Number> numberType, int decimalPositions, boolean disableGrouping) { int decimals = decimalPositions; if (decimals < 0) { // use default, if any if (getLocalization() != null) { decimals = getLocalization().getDefaultDecimalPositions().orElse(-1); } } NumberFormat format = null; if (TypeUtils.isDecimalNumber(numberType)) { format = NumberFormat.getInstance(checkLocalized()); if (decimals > -1) { format.setMinimumFractionDigits(decimals); format.setMaximumFractionDigits(decimals); } } else { format = NumberFormat.getIntegerInstance(checkLocalized()); } if (disableGrouping) { format.setGroupingUsed(false); } return format; }
if (TypeUtils.isDecimalNumber(value.getClass())) { return PARAMETER_VALUE_DECIMAL_FORMAT.format(value); } else {
} else if (TypeUtils.isDecimalNumber(number.getClass())) { format = NumberFormat.getInstance(checkLocalized());
/** * Renders a numeric value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderNumber(Property<? extends T> property) { // Number format Class<? extends Number> type = (Class<? extends Number>) property.getType(); int decimals = property.getConfiguration().getParameter(StringValuePresenter.DECIMAL_POSITIONS).orElse(-1); boolean disableGrouping = property.getConfiguration().getParameter(StringValuePresenter.DISABLE_GROUPING) .orElse(Boolean.FALSE); Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); NumberFormat numberFormat = LocalizationContext.getCurrent().filter(l -> l.isLocalized()) .map((l) -> l.getNumberFormat(type, decimals, disableGrouping)) .orElse(TypeUtils.isDecimalNumber(property.getType()) ? NumberFormat.getNumberInstance(locale) : NumberFormat.getIntegerInstance(locale)); if (decimals > -1) { numberFormat.setMinimumFractionDigits(decimals); numberFormat.setMaximumFractionDigits(decimals); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return postProcessField(input.number(type).numberFormat(numberFormat).asField(), property); }
/** * Renders a numeric value type Field * @param property Property to render * @return Field instance */ @SuppressWarnings("unchecked") protected Field<T> renderNumber(Property<? extends T> property) { // Number format Class<? extends Number> type = (Class<? extends Number>) property.getType(); int decimals = property.getConfiguration().getParameter(StringValuePresenter.DECIMAL_POSITIONS).orElse(-1); boolean disableGrouping = property.getConfiguration().getParameter(StringValuePresenter.DISABLE_GROUPING) .orElse(Boolean.FALSE); Locale locale = LocalizationContext.getCurrent().filter(l -> l.isLocalized()).flatMap(l -> l.getLocale()) .orElse(Locale.getDefault()); NumberFormat numberFormat = LocalizationContext.getCurrent().filter(l -> l.isLocalized()) .map((l) -> l.getNumberFormat(type, decimals, disableGrouping)) .orElse(TypeUtils.isDecimalNumber(property.getType()) ? NumberFormat.getNumberInstance(locale) : NumberFormat.getIntegerInstance(locale)); if (decimals > -1) { numberFormat.setMinimumFractionDigits(decimals); numberFormat.setMaximumFractionDigits(decimals); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return postProcessField(input.number(type).numberFormat(numberFormat).asField(), property); }
() -> TypeUtils.isDecimalNumber(getNumberType()) ? NumberFormat.getNumberInstance(getLocale(context)) : NumberFormat.getIntegerInstance(getLocale(context)));
if (TypeUtils.isDecimalNumber(getType())) { symbols = new char[] { dfs.getGroupingSeparator(), dfs.getDecimalSeparator() }; } else { if (TypeUtils.isDecimalNumber(getType())) { symbols = new char[] { dfs.getDecimalSeparator() };
return ConversionUtils.convertNumberToTargetClass(parsed, (Class<Number>) targetType); if (TypeUtils.isDecimalNumber(targetType)) { Number parsed = FormDataFormats.DECIMAL_FORMAT.parse(value); return ConversionUtils.convertNumberToTargetClass(parsed, (Class<Number>) targetType);
if (TypeUtils.isDecimalNumber(getType())) { symbols = new char[] { dfs.getGroupingSeparator(), dfs.getDecimalSeparator() }; } else { if (TypeUtils.isDecimalNumber(getType())) { symbols = new char[] { dfs.getDecimalSeparator() };
@Override public String getValidationPattern() { if (TypeUtils.isDecimalNumber(getNumberType())) { // decimals final Character decimals = getDecimalSymbol().orElse('.'); // check max decimals final String decimalSuffix = (getMaxDecimals() > -1) ? ("{0," + getMaxDecimals() + "}") : "*"; // base pattern final String decimalPattern = PATTERN_DECIMAL_PREFIX + "(" + escape(decimals) + "|" + escape(decimals) + "[0-9]" + decimalSuffix + ")?"; // check negatives if (isAllowNegatives()) { return PATTERN_NEGATIVE_PREFIX + "|" + decimalPattern + "|" + PATTERN_NEGATIVE_PREFIX + decimalPattern; } return decimalPattern; } else { return isAllowNegatives() ? PATTERN_NEGATIVE_PREFIX + PATTERN_INTEGER : PATTERN_INTEGER; } }