1 /*
2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
497 (number instanceof BigInteger &&
498 ((BigInteger)number).bitLength () < 64)) {
499 return format(((Number)number).longValue(), toAppendTo, pos);
500 } else if (number instanceof BigDecimal) {
501 return format((BigDecimal)number, toAppendTo, pos);
502 } else if (number instanceof BigInteger) {
503 return format((BigInteger)number, toAppendTo, pos);
504 } else if (number instanceof Number) {
505 return format(((Number)number).doubleValue(), toAppendTo, pos);
506 } else {
507 throw new IllegalArgumentException("Cannot format given Object as a Number");
508 }
509 }
510
511 /**
512 * Formats a double to produce a string.
513 * @param number The double to format
514 * @param result where the text is to be appended
515 * @param fieldPosition On input: an alignment field, if desired.
516 * On output: the offsets of the alignment field.
517 * @exception ArithmeticException if rounding is needed with rounding
518 * mode being set to RoundingMode.UNNECESSARY
519 * @return The formatted number string
520 * @see java.text.FieldPosition
521 */
522 @Override
523 public StringBuffer format(double number, StringBuffer result,
524 FieldPosition fieldPosition) {
525 // If fieldPosition is a DontCareFieldPosition instance we can
526 // try to go to fast-path code.
527 boolean tryFastPath = false;
528 if (fieldPosition == DontCareFieldPosition.INSTANCE)
529 tryFastPath = true;
530 else {
531 fieldPosition.setBeginIndex(0);
532 fieldPosition.setEndIndex(0);
533 }
534
535 if (tryFastPath) {
536 String tempResult = fastFormat(number);
615 synchronized(digitList) {
616 int maxIntDigits = super.getMaximumIntegerDigits();
617 int minIntDigits = super.getMinimumIntegerDigits();
618 int maxFraDigits = super.getMaximumFractionDigits();
619 int minFraDigits = super.getMinimumFractionDigits();
620
621 digitList.set(isNegative, number, useExponentialNotation ?
622 maxIntDigits + maxFraDigits : maxFraDigits,
623 !useExponentialNotation);
624 return subformat(result, delegate, isNegative, false,
625 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
626 }
627 }
628
629 /**
630 * Format a long to produce a string.
631 * @param number The long to format
632 * @param result where the text is to be appended
633 * @param fieldPosition On input: an alignment field, if desired.
634 * On output: the offsets of the alignment field.
635 * @exception ArithmeticException if rounding is needed with rounding
636 * mode being set to RoundingMode.UNNECESSARY
637 * @return The formatted number string
638 * @see java.text.FieldPosition
639 */
640 @Override
641 public StringBuffer format(long number, StringBuffer result,
642 FieldPosition fieldPosition) {
643 fieldPosition.setBeginIndex(0);
644 fieldPosition.setEndIndex(0);
645
646 return format(number, result, fieldPosition.getFieldDelegate());
647 }
648
649 /**
650 * Format a long to produce a string.
651 * @param number The long to format
652 * @param result where the text is to be appended
653 * @param delegate notified of locations of sub fields
654 * @return The formatted number string
|
1 /*
2 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
497 (number instanceof BigInteger &&
498 ((BigInteger)number).bitLength () < 64)) {
499 return format(((Number)number).longValue(), toAppendTo, pos);
500 } else if (number instanceof BigDecimal) {
501 return format((BigDecimal)number, toAppendTo, pos);
502 } else if (number instanceof BigInteger) {
503 return format((BigInteger)number, toAppendTo, pos);
504 } else if (number instanceof Number) {
505 return format(((Number)number).doubleValue(), toAppendTo, pos);
506 } else {
507 throw new IllegalArgumentException("Cannot format given Object as a Number");
508 }
509 }
510
511 /**
512 * Formats a double to produce a string.
513 * @param number The double to format
514 * @param result where the text is to be appended
515 * @param fieldPosition On input: an alignment field, if desired.
516 * On output: the offsets of the alignment field.
517 * @exception NullPointerException if {@code result} or
518 * {@code fieldPosition} is {@code null}
519 * @exception ArithmeticException if rounding is needed with rounding
520 * mode being set to RoundingMode.UNNECESSARY
521 * @return The formatted number string
522 * @see java.text.FieldPosition
523 */
524 @Override
525 public StringBuffer format(double number, StringBuffer result,
526 FieldPosition fieldPosition) {
527 // If fieldPosition is a DontCareFieldPosition instance we can
528 // try to go to fast-path code.
529 boolean tryFastPath = false;
530 if (fieldPosition == DontCareFieldPosition.INSTANCE)
531 tryFastPath = true;
532 else {
533 fieldPosition.setBeginIndex(0);
534 fieldPosition.setEndIndex(0);
535 }
536
537 if (tryFastPath) {
538 String tempResult = fastFormat(number);
617 synchronized(digitList) {
618 int maxIntDigits = super.getMaximumIntegerDigits();
619 int minIntDigits = super.getMinimumIntegerDigits();
620 int maxFraDigits = super.getMaximumFractionDigits();
621 int minFraDigits = super.getMinimumFractionDigits();
622
623 digitList.set(isNegative, number, useExponentialNotation ?
624 maxIntDigits + maxFraDigits : maxFraDigits,
625 !useExponentialNotation);
626 return subformat(result, delegate, isNegative, false,
627 maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
628 }
629 }
630
631 /**
632 * Format a long to produce a string.
633 * @param number The long to format
634 * @param result where the text is to be appended
635 * @param fieldPosition On input: an alignment field, if desired.
636 * On output: the offsets of the alignment field.
637 * @exception NullPointerException if {@code result} or
638 * {@code fieldPosition} is {@code null}
639 * @exception ArithmeticException if rounding is needed with rounding
640 * mode being set to RoundingMode.UNNECESSARY
641 * @return The formatted number string
642 * @see java.text.FieldPosition
643 */
644 @Override
645 public StringBuffer format(long number, StringBuffer result,
646 FieldPosition fieldPosition) {
647 fieldPosition.setBeginIndex(0);
648 fieldPosition.setEndIndex(0);
649
650 return format(number, result, fieldPosition.getFieldDelegate());
651 }
652
653 /**
654 * Format a long to produce a string.
655 * @param number The long to format
656 * @param result where the text is to be appended
657 * @param delegate notified of locations of sub fields
658 * @return The formatted number string
|