Example usage for java.text NumberFormat parseObject

List of usage examples for java.text NumberFormat parseObject

Introduction

In this page you can find the example usage for java.text NumberFormat parseObject.

Prototype

@Override
public final Object parseObject(String source, ParsePosition pos) 

Source Link

Document

Parses text from a string to produce a Number.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    System.out.println(numberFormat.parseObject("123", new ParsePosition(0)));
}

From source file:Main.java

private static Format createFormat() {
    NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly(true);//from   w  ww  .ja  v a2 s.c  o  m
    return new Format() {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            return format.format(obj, toAppendTo, pos);
        }

        @Override
        public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
            return format.formatToCharacterIterator(obj);
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            int initialIndex = pos.getIndex();
            Object result = format.parseObject(source, pos);
            if (result != null && pos.getIndex() > initialIndex + 1) {
                int errorIndex = initialIndex + 1;
                pos.setIndex(initialIndex);
                pos.setErrorIndex(errorIndex);
                return null;
            }
            return result;
        }
    };
}