/** * Create a default number format. The default number format is based on * {@link NumberFormat#getNumberInstance(java.util.Locale)}. The only * customization is the maximum number of BigFraction digits, which is set to 0. * @param locale the specific locale used by the format. * @return the default number format specific to the given locale. */ protected static NumberFormat getDefaultNumberFormat(final Locale locale) { final NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setMaximumFractionDigits(0); nf.setParseIntegerOnly(true); return nf; }
private SimpleDateFormat(Locale locale) { numberFormat = NumberFormat.getInstance(locale); numberFormat.setParseIntegerOnly(true); numberFormat.setGroupingUsed(false); calendar = new GregorianCalendar(locale); calendar.add(Calendar.YEAR, -80); creationYear = calendar.get(Calendar.YEAR); defaultCenturyStart = calendar.getTime(); }
/** * Returns a {@code NumberFormat} for formatting and parsing integers for * the specified locale. * * @param locale * the locale to use. * @return a {@code NumberFormat} for handling integers. */ public static NumberFormat getIntegerInstance(Locale locale) { NumberFormat result = getInstance(LocaleData.get(locale).integerPattern, locale); result.setParseIntegerOnly(true); return result; }
try { NumberFormat nf = NumberFormat.getIntegerInstance(locale); nf.setParseIntegerOnly(true); nf.setMaximumIntegerDigits(9); // Or whatever you'd like to max out at. // Start parsing from the beginning. ParsePosition p = new ParsePosition(0); int val = format.parse(str, p).intValue(); if (p.getIndex() != str.length()) { // There's some stuff after all the digits are done being processed. } // Work with the processed value here. } catch (java.text.ParseFormatException exc) { // Something blew up in the parsing. }
try { NumberFormat format = NumberFormat.getIntegerInstance(locale); format.setParseIntegerOnly(true); format.setMaximumIntegerDigits(9); ParsePosition pos = new ParsePosition(0); int val = format.parse(str, pos).intValue(); if (pos.getIndex() != str.length()) { // ... handle case of extraneous characters after digits ... } // ... use val ... } catch (java.text.ParseFormatException exc) { // ... handle this case appropriately ... }
/** * Checks if the value can safely be converted to a short primitive. * * @param value The value validation is being performed on. * @param locale The locale to use to parse the number (system default if * null) * @return the converted Short value. */ public static Short formatShort(String value, Locale locale) { Short result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() && num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) { result = Short.valueOf(num.shortValue()); } } return result; }
/** * Checks if the value can safely be converted to a long primitive. * * @param value The value validation is being performed on. * @param locale The locale to use to parse the number (system default if * null) * @return the converted Long value. */ public static Long formatLong(String value, Locale locale) { Long result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() && num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) { result = Long.valueOf(num.longValue()); } } return result; }
/** * Checks if the value can safely be converted to an int primitive. * * @param value The value validation is being performed on. * @param locale The locale to use to parse the number (system default if * null) * @return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() && num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = Integer.valueOf(num.intValue()); } } return result; }
/** * Checks if the value can safely be converted to a byte primitive. * * @param value The value validation is being performed on. * @param locale The locale to use to parse the number (system default if * null) * @return the converted Byte value. */ public static Byte formatByte(String value, Locale locale) { Byte result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() && num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) { result = Byte.valueOf(num.byteValue()); } } return result; }
/** * <p>Returns a <code>NumberFormat</code> for the specified <i>pattern</i> * and/or <code>Locale</code>.</p> * * @param pattern The pattern used to validate the value against or * <code>null</code> to use the default for the <code>Locale</code>. * @param locale The locale to use for the currency format, system default if null. * @return The <code>NumberFormat</code> to created. */ @Override protected Format getFormat(String pattern, Locale locale) { NumberFormat formatter = null; boolean usePattern = (pattern != null && pattern.length() > 0); if (!usePattern) { formatter = (NumberFormat)getFormat(locale); } else if (locale == null) { formatter = new DecimalFormat(pattern); } else { DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale); formatter = new DecimalFormat(pattern, symbols); } if (determineScale(formatter) == 0) { formatter.setParseIntegerOnly(true); } return formatter; }
format.setParseIntegerOnly(true);
nf.setParseIntegerOnly(onlyInteger); nf.parse(input, pp); if (pp.getIndex() != (input.length())) {
@Override protected NumberFormat newNumberFormat(Locale locale) { NumberFormat numberFormat = NumberFormat.getIntegerInstance(locale); numberFormat.setParseIntegerOnly(true); numberFormat.setGroupingUsed(false); return numberFormat; } }
private SimpleDateFormat(Locale locale) { numberFormat = NumberFormat.getInstance(locale); numberFormat.setParseIntegerOnly(true); numberFormat.setGroupingUsed(false); calendar = new GregorianCalendar(locale); calendar.add(Calendar.YEAR, -80); creationYear = calendar.get(Calendar.YEAR); defaultCenturyStart = calendar.getTime(); }
public static void main(final String[] args) throws ParseException { NumberFormat formatter = NumberFormat.getInstance(); formatter.setParseIntegerOnly(true); System.out.println("Test"); final String nombre = "3.0"; int entier; entier=formatter.parse(nombre).intValue(); System.out.println("result :" + entier); }
protected void buildFormat(final boolean _sci){ super.buildFormat(false); format_.setParseIntegerOnly(true); } }
public NumericField (int init) { super (numFormat); numFormat.setGroupingUsed (false); numFormat.setParseIntegerOnly (true); setValue (new Integer (init)); } }
/** * {@inheritDoc} */ @Override public NumberFormat getIntegerInstance(final Locale locale) throws IllegalArgumentException, NullPointerException { NumberFormat format = getInstance("IntegerPattern", locale); format.setParseIntegerOnly(true); return format; }
/** * Restricted constructor. */ private NumberFormatter(NumberFormat format) { ArgChecker.notNull(format, "format"); format.setParseIntegerOnly(false); this.underlying = ThreadLocal.withInitial(() -> (NumberFormat) format.clone()); }