Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos);

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();
    numberFormat.format(111L, sb, new FieldPosition(0));
    System.out.println(sb);//from   w w  w . j a va 2s  .  c om

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();
    numberFormat.format(2.234, sb, new FieldPosition(0));
    System.out.println(sb);//  ww w  . j  a v  a2 s  . co  m

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();

    Object value = 111L;/*from   w w w .ja va 2 s .c o m*/
    numberFormat.format(value, sb, new FieldPosition(0));
    System.out.println(sb);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getPercentInstance();

    StringBuffer sb = new StringBuffer();

    Object value = 11.111;/*from  w w  w.j a va 2  s. c  o  m*/
    numberFormat.format(value, sb, new FieldPosition(NumberFormat.FRACTION_FIELD));
    System.out.println(sb);

}

From source file:Main.java

private static Format createFormat() {
    NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly(true);/*  w w w.j  a  v  a 2s . c  om*/
    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;
        }
    };
}