public static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); return str.length() == pos.getIndex(); }
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE); Number number = format.parse("1,234"); double d = number.doubleValue();
@Override public Number parse(String text, Locale locale) throws ParseException { NumberFormat format = getNumberFormat(locale); ParsePosition position = new ParsePosition(0); Number number = format.parse(text, position); if (position.getErrorIndex() != -1) { throw new ParseException(text, position.getIndex()); } if (!this.lenient) { if (text.length() != position.getIndex()) { // indicates a part of the string that was not parsed throw new ParseException(text, position.getIndex()); } } return number; }
import java.text.NumberFormat; import java.text.ParseException; public class MyVisitor { public static void main(String[] args) throws ParseException { System.out.println(NumberFormat.getNumberInstance().parse("61412356123125124")); } }
import java.text.*; import java.util.*; public class Test { // Just for the sake of a simple test program! public static void main(String[] args) throws Exception { NumberFormat format = NumberFormat.getInstance(Locale.US); Number number = format.parse("835,111.2"); System.out.println(number); // or use number.doubleValue() } }
import java.text.*; public class Test { public static void main(String[] args) { System.out.println(parseCompleteString("12")); System.out.println(parseCompleteString("12abc")); System.out.println(parseCompleteString("abc12")); } public static Number parseCompleteString(String input) { ParsePosition pp = new ParsePosition(0); NumberFormat numberFormat = new DecimalFormat(); Number result = numberFormat.parse(input, pp); return pp.getIndex() == input.length() ? result : null; } }
NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); if(str.length() != pos.getIndex()){ System.out.println("error"); }
/** * 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; }
/** * Parses a string with a locale and returns the corresponding number * * @throws ParseException if number cannot be parsed */ public static double parseNumber(String number, Locale locale) throws ParseException { if ("".equals(number)) { return Double.NaN; } return NumberFormat.getNumberInstance(locale).parse(number).doubleValue(); }
String value1 = "1,222.34"; String value2 = "2,334.45"; double d1 = 0; double d2 = 0; try { NumberFormat usFormat = NumberFormat.getNumberInstance(Locale.US); d1 = usFormat.parse(value1).doubleValue(); d2 = usFormat.parse(value2).doubleValue(); } catch(ParseException ex) { ex.printStackTrace(); } System.out.println(d1 + d2); // Output 3556.79
// Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#"; DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols); decimalFormat.setParseBigDecimal(true); // parse the string BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse("10,692,467,440,017.120"); System.out.println(bigDecimal);
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN); df.setParseBigDecimal(true); BigDecimal bd = (BigDecimal) df.parseObject(numberString); System.out.println(bd.toString()); } catch (ParseException e) { e.printStackTrace(); NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); try { BigDecimal bd1 = new BigDecimal(nf.parse(numberString).toString()); System.out.println(bd1.toString()); } catch (ParseException e) { e.printStackTrace(); System.out.println(new BigDecimal(numberStringFixed));; System.out.println(new BigDecimal(numberString));;
NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE); nf.parse(p);
/** * Checks if the value can safely be converted to a double 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 Double value. */ public static Double formatDouble(String value, Locale locale) { Double result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getInstance(locale); } else { formatter = NumberFormat.getInstance(Locale.getDefault()); } 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() >= (Double.MAX_VALUE * -1) && num.doubleValue() <= Double.MAX_VALUE) { result = new Double(num.doubleValue()); } } 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; }
NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")
@Override public Number parse(String text, Locale locale) throws ParseException { NumberFormat format = getNumberFormat(locale); ParsePosition position = new ParsePosition(0); Number number = format.parse(text, position); if (position.getErrorIndex() != -1) { throw new ParseException(text, position.getIndex()); } if (!this.lenient) { if (text.length() != position.getIndex()) { // indicates a part of the string that was not parsed throw new ParseException(text, position.getIndex()); } } return number; }
NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858"); int test = 0; try { test = NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858").intValue(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("" + test);