1 package org.simantics.spreadsheet.graph.formula;
3 import org.simantics.spreadsheet.graph.CellFormulaFunction;
4 import org.simantics.spreadsheet.graph.CellValueVisitor;
5 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
6 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
8 public class RoundFormulaFunction implements CellFormulaFunction<Object> {
11 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
12 if (args.values.size() != 2) throw new IllegalStateException();
14 Object number = args.values.get(0).accept(visitor);
15 Object digits = args.values.get(1).accept(visitor);
17 FormulaError2 err1 = FormulaError2.forObject(number);
18 if(err1!=null) return err1.getString();
19 FormulaError2 err2 = FormulaError2.forObject(digits);
20 if(err2!=null) return err2.getString();
22 double n = SpreadsheetGraphUtils.asNumber(number);
23 double n2 = SpreadsheetGraphUtils.asNumber(digits);
24 Double scale = Math.pow(10, n2);
26 long l = Math.round(scale * n);
27 Double d = Double.valueOf(((double) l) / scale);