--- /dev/null
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.OperationEvaluationContext;\r
+import org.apache.poi.ss.formula.eval.*;\r
+\r
+/**\r
+ * Implementation for Excel Bin2Dec() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>Bin2Dec </b>(<b>number</b>,<b>[places]</b> )<br/>\r
+ * <p/>\r
+ * Converts a decimal number to binary.\r
+ * <p/>\r
+ * The DEC2BIN function syntax has the following arguments:\r
+ * <ul>\r
+ * <li>Number Required. The decimal integer you want to convert. If number is negative, valid place values are ignored and DEC2BIN returns a 10-character (10-bit) binary number in which the most significant bit is the sign bit. The remaining 9 bits are magnitude bits. Negative numbers are represented using two's-complement notation.</li>\r
+ * <li>Places Optional. The number of characters to use. If places is omitted, DEC2BIN uses the minimum number of characters necessary. Places is useful for padding the return value with leading 0s (zeros).</li>\r
+ * </ul>\r
+ * <p/>\r
+ * Remarks\r
+ * <ul>\r
+ * <li>If number < -512 or if number > 511, DEC2BIN returns the #NUM! error value.</li>\r
+ * <li>If number is nonnumeric, DEC2BIN returns the #VALUE! error value.</li>\r
+ * <li>If DEC2BIN requires more than places characters, it returns the #NUM! error value.</li>\r
+ * <li>If places is not an integer, it is truncated.</li>\r
+ * <li>If places is nonnumeric, DEC2BIN returns the #VALUE! error value.</li>\r
+ * <li>If places is zero or negative, DEC2BIN returns the #NUM! error value.</li>\r
+ * </ul>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class Dec2Bin extends Var1or2ArgFunction implements FreeRefFunction {\r
+\r
+ public static final FreeRefFunction instance = new Dec2Bin();\r
+\r
+ private final static long MIN_VALUE = Long.parseLong("-512");\r
+ private final static long MAX_VALUE = Long.parseLong("512");\r
+ private final static int DEFAULT_PLACES_VALUE = 10;\r
+\r
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval placesVE) {\r
+ ValueEval veText1;\r
+ try {\r
+ veText1 = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);\r
+ } catch (EvaluationException e) {\r
+ return e.getErrorEval();\r
+ }\r
+ String strText1 = OperandResolver.coerceValueToString(veText1);\r
+ Double number = OperandResolver.parseDouble(strText1);\r
+\r
+ //If this number argument is non numeric, this function returns the #VALUE! error value.\r
+ if (number == null) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+\r
+ //If number < -512 or if number > 512, this function returns the #NUM! error value.\r
+ if (number.longValue() < MIN_VALUE || number.longValue() > MAX_VALUE) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ int placesNumber;\r
+ if (number < 0 || placesVE == null) {\r
+ placesNumber = DEFAULT_PLACES_VALUE;\r
+ } else {\r
+ ValueEval placesValueEval;\r
+ try {\r
+ placesValueEval = OperandResolver.getSingleValue(placesVE, srcRowIndex, srcColumnIndex);\r
+ } catch (EvaluationException e) {\r
+ return e.getErrorEval();\r
+ }\r
+ String placesStr = OperandResolver.coerceValueToString(placesValueEval);\r
+ Double placesNumberDouble = OperandResolver.parseDouble(placesStr);\r
+\r
+ //non numeric value\r
+ if (placesNumberDouble == null) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+\r
+ //If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.\r
+ placesNumber = placesNumberDouble.intValue();\r
+\r
+ if (placesNumber < 0 || placesNumber == 0) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+ }\r
+ String binary = Integer.toBinaryString(number.intValue());\r
+\r
+ if (binary.length() > DEFAULT_PLACES_VALUE) {\r
+ binary = binary.substring(binary.length() - DEFAULT_PLACES_VALUE, binary.length());\r
+ }\r
+ //If DEC2BIN requires more than places characters, it returns the #NUM! error value.\r
+ if (binary.length() > placesNumber) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ return new StringEval(binary);\r
+ }\r
+\r
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {\r
+ return this.evaluate(srcRowIndex, srcColumnIndex, numberVE, null);\r
+ }\r
+\r
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+ if (args.length == 1) {\r
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);\r
+ }\r
+ if (args.length == 2) {\r
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);\r
+ }\r
+\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+}\r