You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFNumber.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import org.apache.fop.util.DecimalFormatCache;
  20. /**
  21. * This class represents a simple number object. It also contains contains some
  22. * utility methods for outputting numbers to PDF.
  23. */
  24. public class PDFNumber extends PDFObject {
  25. private Number number;
  26. /**
  27. * Returns the number.
  28. * @return the number
  29. */
  30. public Number getNumber() {
  31. return this.number;
  32. }
  33. /**
  34. * Sets the number.
  35. * @param number the number
  36. */
  37. public void setNumber(Number number) {
  38. this.number = number;
  39. }
  40. /**
  41. * Output a Double value to a string suitable for PDF.
  42. *
  43. * @param doubleDown the Double value
  44. * @return the value as a string
  45. */
  46. public static String doubleOut(Double doubleDown) {
  47. return doubleOut(doubleDown.doubleValue());
  48. }
  49. /**
  50. * Output a double value to a string suitable for PDF (6 decimal digits).
  51. *
  52. * @param doubleDown the double value
  53. * @return the value as a string
  54. */
  55. public static String doubleOut(double doubleDown) {
  56. return doubleOut(doubleDown, 6);
  57. }
  58. /**
  59. * Output a double value to a string suitable for PDF.
  60. * In this method it is possible to set the maximum
  61. * number of decimal places to output.
  62. *
  63. * @param doubleDown the Double value
  64. * @param dec the number of decimal places to output
  65. * @return the value as a string
  66. */
  67. public static String doubleOut(double doubleDown, int dec) {
  68. return DecimalFormatCache.getDecimalFormat(dec).format(doubleDown);
  69. }
  70. /** {@inheritDoc} */
  71. protected String toPDFString() {
  72. if (getNumber() == null) {
  73. throw new IllegalArgumentException(
  74. "The number of this PDFNumber must not be empty");
  75. }
  76. StringBuffer sb = new StringBuffer(64);
  77. sb.append(doubleOut(getNumber().doubleValue(), 10));
  78. return sb.toString();
  79. }
  80. }