Package java.text

Examples of java.text.FieldPosition


            // form the replacement string
            MessageFormat linkFormat = feelinLucky ? getLuckyLinkFormat() : getLinkFormat();
            StringBuffer replacement = new StringBuffer(128);
            args[2] = linkText;
            args[3] = encodedSearchText;
            linkFormat.format(args, replacement, new FieldPosition(0));

            // append replacement
            m.appendReplacement(result, replacement.toString());
        }
        m.appendTail(result);
View Full Code Here


     */
    protected String generateLink(MessageFormat fmt, String site, String tag)
    {
        // Allocate initial capacity of buffer of approximately the right length.
        StringBuffer sb = new StringBuffer(site.length() + tag.length() + getLinkFormatString().length());
        fmt.format(new Object[]{site, urlEncode(tag), tag}, sb, new FieldPosition(0));
        return sb.toString();
    }
View Full Code Here

        // may be exchanged while we are trying to use it
        MessageFormat myFormat = format;
        synchronized (myFormat) {
            myFormat.format(new Object[] { new Date(), marker,
                Thread.currentThread().getName(), name, level.toString(), msg,
                fqcn }, buffer, new FieldPosition(0));
        }
    }
View Full Code Here

        DecimalFormat form = (DecimalFormat) nform;

        // If Object(including null) is not of type Number,
        // IllegalArgumentException will be thrown out
        try {
            form.format(new Object(), new StringBuffer(), new FieldPosition(0));
            fail("Should throw IAE");
        } catch (IllegalArgumentException e) {
            // expected
        }
        try {
            form.format(null, new StringBuffer(), new FieldPosition(0));
            fail("Should throw IAE");
        } catch (IllegalArgumentException e) {
            // expected
        }

        // When StringBuffer == null || FieldPosition == null
        // NullPointerException will be thrown out.
        try {
            form.format(new Double(1.9), null, new FieldPosition(0));
            fail("Should throw NPE");
        } catch (NullPointerException e) {
            // expected
        }

        try {
            form.format(new Double(1.3), new StringBuffer(), null);
            fail("Should throw NPE");
        } catch (NullPointerException e) {
            // expected
        }

        try {
            form.format(new Double(1.4), null, null);
            fail("Should throw NPE");
        } catch (NullPointerException e) {
            // expected
        }

        try {
            form.format(new Object(), null, null);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }

        FieldPosition pos;
        StringBuffer out;
        DecimalFormat format = (DecimalFormat) NumberFormat
                .getInstance(Locale.US);

        // format maxLong
        pos = new FieldPosition(0);
        out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
        assertTrue("Wrong result L1: " + out, out.toString().equals(
                "9,223,372,036,854,775,807"));

        // format minLong
        pos = new FieldPosition(0);
        out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
        assertTrue("Wrong result L2: " + out, out.toString().equals(
                "-9,223,372,036,854,775,808"));

        // format maxLong of type BigInteger
        pos = new FieldPosition(0);
        out = format.format(new java.math.BigInteger(String
                .valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
        assertTrue("Wrong result BI1: " + out, out.toString().equals(
                "9,223,372,036,854,775,807"));

        // format minLong of type BigInteger
        pos = new FieldPosition(0);
        out = format.format(new java.math.BigInteger(String
                .valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
        assertTrue("Wrong result BI2: " + out, out.toString().equals(
                "-9,223,372,036,854,775,808"));

        // format maxLong + 1
        java.math.BigInteger big;
        pos = new FieldPosition(0);
        big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE))
                .add(new java.math.BigInteger("1"));
        out = format.format(big, new StringBuffer(), pos);
        assertTrue("Wrong result BI3: " + out, out.toString().equals(
                "9,223,372,036,854,775,808"));

        // format minLong - 1
        pos = new FieldPosition(0);
        big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE))
                .add(new java.math.BigInteger("-1"));
        out = format.format(big, new StringBuffer(), pos);
        assertTrue("Wrong result BI4: " + out, out.toString().equals(
                "-9,223,372,036,854,775,809"));

        // format big decimal
        pos = new FieldPosition(0);
        out = format.format(new java.math.BigDecimal("51.348"),
                new StringBuffer(), pos);
        assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348"));

        // format big decimal
        pos = new FieldPosition(0);
        out = format.format(new java.math.BigDecimal("51"), new StringBuffer(),
                pos);
        assertTrue("Wrong result BD2: " + out, out.toString().equals("51"));

        // format big decimal Double.MAX_VALUE * 2
        java.math.BigDecimal bigDecimal;
        pos = new FieldPosition(0);
        final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408,"
                + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077,"
                + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653,"
                + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152,"
                + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806,"
                + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476,"
                + "354,361,838,599,762,500,808,052,368,249,716,736";
        bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(
                Double.MAX_VALUE));
        out = format.format(bigDecimal, new StringBuffer(), pos);
        assertTrue("Wrong result BDmax2: " + out, out.toString().equals(
                doubleMax2));

        // format big decimal Double.MIN_VALUE + Double.MIN_VALUE
        // and Double.MIN_VALUE - Double.MIN_VALUE
        pos = new FieldPosition(0);

        bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(
                Double.MIN_VALUE));
        out = format.format(bigDecimal, new StringBuffer(), pos);

View Full Code Here

    public void test_setGroupingUse() {
        DecimalFormat format = new DecimalFormat();
        StringBuffer buf = new StringBuffer();
        format.setGroupingUsed(false);
        format.format(new Long(1970), buf, new FieldPosition(0));
        assertEquals("1970", buf.toString());
        assertFalse(format.isGroupingUsed());
    }
View Full Code Here

    decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);

    try {
      // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException
      decimalFormat
          .format(11.5, new StringBuffer(), new FieldPosition(0));
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
      // expected
    }
  }
View Full Code Here

    decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY);
    try {
      // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException
      decimalFormat.format(99999, new StringBuffer(),
          new FieldPosition(0));
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
      // expected
    }
  }
View Full Code Here

    StringBuffer formattedAmount = new StringBuffer();

    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
    DecimalFormat format = new DecimalFormat("0.00", symbols);
    format.format(amount, formattedAmount, new FieldPosition(0));

    String xmlMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        + "<charge-order xmlns=\"" + NAMESPACE
        + "\" google-order-number=\"" + orderNumber + "\">\n"
        + " <amount currency=\"USD\">" + formattedAmount.toString()
View Full Code Here

    }

    StringBuffer formattedAmount = new StringBuffer();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
    DecimalFormat format = new DecimalFormat("0.00", symbols);
    format.format(amount, formattedAmount, new FieldPosition(0));

    String xmlMessage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        + "<refund-order xmlns=\"" + NAMESPACE
        + "\" google-order-number=\"" + orderNumber + "\">\n"
        + " <amount currency=\"USD\">" + formattedAmount.toString()
View Full Code Here

    private void writeScientific(double value, StringBuffer output,
            Set<StringMod> mods) {

        StringBuffer result = new StringBuffer();
        FieldPosition fractionPos = new FieldPosition(
                DecimalFormat.FRACTION_FIELD);
        decimalFmt.format(value, result, fractionPos);
        writeInteger(result, output, integerSpecials, mods, integerCommas);
        writeFractional(result, output);

        /*
        * Exponent sign handling is complex.
        *
        * In DecimalFormat, you never put the sign in the format, and the sign only
        * comes out of the format if it is negative.
        *
        * In Excel, you always say whether to always show the sign ("e+") or only
        * show negative signs ("e-").
        *
        * Also in Excel, where you put the sign in the format is NOT where it comes
        * out in the result.  In the format, the sign goes with the "e"; in the
        * output it goes with the exponent value.  That is, if you say "#e-|#" you
        * get "1e|-5", not "1e-|5". This makes sense I suppose, but it complicates
        * things.
        *
        * Finally, everything else in this formatting code assumes that the base of
        * the result is the original format, and that starting from that situation,
        * the indexes of the original special characters can be used to place the new
        * characters.  As just described, this is not true for the exponent's sign.
        * <p/>
        * So here is how we handle it:
        *
        * (1) When parsing the format, remove the sign from after the 'e' and put it
        * before the first digit of the exponent (where it will be shown).
        *
        * (2) Determine the result's sign.
        *
        * (3) If it's missing, put the sign into the output to keep the result
        * lined up with the output. (In the result, "after the 'e'" and "before the
        * first digit" are the same because the result has no extra chars to be in
        * the way.)
        *
        * (4) In the output, remove the sign if it should not be shown ("e-" was used
        * and the sign is negative) or set it to the correct value.
        */

        // (2) Determine the result's sign.
        int ePos = fractionPos.getEndIndex();
        int signPos = ePos + 1;
        char expSignRes = result.charAt(signPos);
        if (expSignRes != '-') {
            // not a sign, so it's a digit, and therefore a positive exponent
            expSignRes = '+';
View Full Code Here

TOP

Related Classes of java.text.FieldPosition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.