Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CellGeneralFormatter.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.format;
  16. import java.util.Formatter;
  17. import java.util.Locale;
  18. import org.apache.poi.util.LocaleUtil;
  19. /**
  20. * A formatter for the default "General" cell format.
  21. *
  22. * @author Ken Arnold, Industrious Media LLC
  23. */
  24. public class CellGeneralFormatter extends CellFormatter {
  25. /** Creates a new general formatter. */
  26. public CellGeneralFormatter() {
  27. this(LocaleUtil.getUserLocale());
  28. }
  29. /** Creates a new general formatter. */
  30. public CellGeneralFormatter(Locale locale) {
  31. super(locale, "General");
  32. }
  33. /**
  34. * The general style is not quite the same as any other, or any combination
  35. * of others.
  36. *
  37. * @param toAppendTo The buffer to append to.
  38. * @param value The value to format.
  39. */
  40. public void formatValue(StringBuffer toAppendTo, Object value) {
  41. if (value instanceof Number) {
  42. double val = ((Number) value).doubleValue();
  43. if (val == 0) {
  44. toAppendTo.append('0');
  45. return;
  46. }
  47. String fmt;
  48. double exp = Math.log10(Math.abs(val));
  49. boolean stripZeros = true;
  50. if (exp > 10 || exp < -9)
  51. fmt = "%1.5E";
  52. else if ((long) val != val)
  53. fmt = "%1.9f";
  54. else {
  55. fmt = "%1.0f";
  56. stripZeros = false;
  57. }
  58. Formatter formatter = new Formatter(toAppendTo, locale);
  59. try {
  60. formatter.format(locale, fmt, value);
  61. } finally {
  62. formatter.close();
  63. }
  64. if (stripZeros) {
  65. // strip off trailing zeros
  66. int removeFrom;
  67. if (fmt.endsWith("E"))
  68. removeFrom = toAppendTo.lastIndexOf("E") - 1;
  69. else
  70. removeFrom = toAppendTo.length() - 1;
  71. while (toAppendTo.charAt(removeFrom) == '0') {
  72. toAppendTo.deleteCharAt(removeFrom--);
  73. }
  74. if (toAppendTo.charAt(removeFrom) == '.') {
  75. toAppendTo.deleteCharAt(removeFrom--);
  76. }
  77. }
  78. } else if (value instanceof Boolean) {
  79. toAppendTo.append(value.toString().toUpperCase(Locale.ROOT));
  80. } else {
  81. toAppendTo.append(value);
  82. }
  83. }
  84. /** Equivalent to {@link #formatValue(StringBuffer,Object)}. {@inheritDoc}. */
  85. public void simpleValue(StringBuffer toAppendTo, Object value) {
  86. formatValue(toAppendTo, value);
  87. }
  88. }