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.

ExcelGeneralNumberFormat.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 2012 - Alfresco Software, Ltd.
  15. Alfresco Software has modified source of this file
  16. The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
  17. ==================================================================== */
  18. package org.apache.poi.ss.usermodel;
  19. import java.math.BigDecimal;
  20. import java.math.MathContext;
  21. import java.math.RoundingMode;
  22. import java.text.DecimalFormat;
  23. import java.text.DecimalFormatSymbols;
  24. import java.text.FieldPosition;
  25. import java.text.Format;
  26. import java.text.ParsePosition;
  27. import java.util.Locale;
  28. /**
  29. * A format that formats a double as Excel would, ignoring FieldPosition.
  30. * All other operations are unsupported.
  31. **/
  32. public class ExcelGeneralNumberFormat extends Format {
  33. private static final long serialVersionUID = 1L;
  34. private static final MathContext TO_10_SF = new MathContext(10, RoundingMode.HALF_UP);
  35. private final DecimalFormatSymbols decimalSymbols;
  36. private final DecimalFormat integerFormat;
  37. private final DecimalFormat decimalFormat;
  38. private final DecimalFormat scientificFormat;
  39. public ExcelGeneralNumberFormat(final Locale locale) {
  40. decimalSymbols = DecimalFormatSymbols.getInstance(locale);
  41. scientificFormat = new DecimalFormat("0.#####E0", decimalSymbols);
  42. DataFormatter.setExcelStyleRoundingMode(scientificFormat);
  43. integerFormat = new DecimalFormat("#", decimalSymbols);
  44. DataFormatter.setExcelStyleRoundingMode(integerFormat);
  45. decimalFormat = new DecimalFormat("#.##########", decimalSymbols);
  46. DataFormatter.setExcelStyleRoundingMode(decimalFormat);
  47. }
  48. @SuppressWarnings("squid:S2111")
  49. public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
  50. final double value;
  51. if (number instanceof Number) {
  52. value = ((Number)number).doubleValue();
  53. if (Double.isInfinite(value) || Double.isNaN(value)) {
  54. return integerFormat.format(number, toAppendTo, pos);
  55. }
  56. } else {
  57. // testBug54786 gets here with a date, so retain previous behaviour
  58. return integerFormat.format(number, toAppendTo, pos);
  59. }
  60. final double abs = Math.abs(value);
  61. if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
  62. return scientificFormat.format(number, toAppendTo, pos);
  63. } else if (Math.floor(value) == value || abs >= 1E10) {
  64. // integer, or integer portion uses all 11 allowed digits
  65. return integerFormat.format(number, toAppendTo, pos);
  66. }
  67. // Non-integers of non-scientific magnitude are formatted as "up to 11
  68. // numeric characters, with the decimal point counting as a numeric
  69. // character". We know there is a decimal point, so limit to 10 digits.
  70. // https://support.microsoft.com/en-us/kb/65903
  71. final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
  72. return decimalFormat.format(rounded, toAppendTo, pos);
  73. }
  74. public Object parseObject(String source, ParsePosition pos) {
  75. throw new UnsupportedOperationException();
  76. }
  77. }