您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Fixed.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.formula.functions;
  16. import java.math.BigDecimal;
  17. import java.math.RoundingMode;
  18. import java.text.DecimalFormat;
  19. import java.text.NumberFormat;
  20. import java.util.Locale;
  21. import org.apache.poi.ss.formula.eval.BoolEval;
  22. import org.apache.poi.ss.formula.eval.ErrorEval;
  23. import org.apache.poi.ss.formula.eval.EvaluationException;
  24. import org.apache.poi.ss.formula.eval.NumberEval;
  25. import org.apache.poi.ss.formula.eval.OperandResolver;
  26. import org.apache.poi.ss.formula.eval.StringEval;
  27. import org.apache.poi.ss.formula.eval.ValueEval;
  28. public final class Fixed implements Function1Arg, Function2Arg, Function3Arg {
  29. @Override
  30. public ValueEval evaluate(
  31. int srcRowIndex, int srcColumnIndex,
  32. ValueEval arg0, ValueEval arg1, ValueEval arg2) {
  33. return fixed(arg0, arg1, arg2, srcRowIndex, srcColumnIndex);
  34. }
  35. @Override
  36. public ValueEval evaluate(
  37. int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  38. return fixed(arg0, arg1, BoolEval.FALSE, srcRowIndex, srcColumnIndex);
  39. }
  40. @Override
  41. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
  42. return fixed(arg0, new NumberEval(2), BoolEval.FALSE, srcRowIndex, srcColumnIndex);
  43. }
  44. @Override
  45. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  46. switch (args.length) {
  47. case 1:
  48. return fixed(args[0], new NumberEval(2), BoolEval.FALSE,
  49. srcRowIndex, srcColumnIndex);
  50. case 2:
  51. return fixed(args[0], args[1], BoolEval.FALSE,
  52. srcRowIndex, srcColumnIndex);
  53. case 3:
  54. return fixed(args[0], args[1], args[2], srcRowIndex, srcColumnIndex);
  55. }
  56. return ErrorEval.VALUE_INVALID;
  57. }
  58. private ValueEval fixed(
  59. ValueEval numberParam, ValueEval placesParam,
  60. ValueEval skipThousandsSeparatorParam,
  61. int srcRowIndex, int srcColumnIndex) {
  62. try {
  63. ValueEval numberValueEval =
  64. OperandResolver.getSingleValue(
  65. numberParam, srcRowIndex, srcColumnIndex);
  66. BigDecimal number =
  67. new BigDecimal(OperandResolver.coerceValueToDouble(numberValueEval));
  68. ValueEval placesValueEval =
  69. OperandResolver.getSingleValue(
  70. placesParam, srcRowIndex, srcColumnIndex);
  71. int places = OperandResolver.coerceValueToInt(placesValueEval);
  72. ValueEval skipThousandsSeparatorValueEval =
  73. OperandResolver.getSingleValue(
  74. skipThousandsSeparatorParam, srcRowIndex, srcColumnIndex);
  75. Boolean skipThousandsSeparator =
  76. OperandResolver.coerceValueToBoolean(
  77. skipThousandsSeparatorValueEval, false);
  78. // Round number to respective places.
  79. number = number.setScale(places, RoundingMode.HALF_UP);
  80. // Format number conditionally using a thousands separator.
  81. NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
  82. DecimalFormat formatter = (DecimalFormat)nf;
  83. formatter.setGroupingUsed(! skipThousandsSeparator);
  84. formatter.setMinimumFractionDigits(places >= 0 ? places : 0);
  85. formatter.setMaximumFractionDigits(places >= 0 ? places : 0);
  86. String numberString = formatter.format(number.doubleValue());
  87. // Return the result as a StringEval.
  88. return new StringEval(numberString);
  89. } catch (EvaluationException e) {
  90. return e.getErrorEval();
  91. }
  92. }
  93. }