]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/RoundFormulaFunction.java
Improved exception message
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / RoundFormulaFunction.java
1 package org.simantics.spreadsheet.graph.formula;
2
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;
7
8 public class RoundFormulaFunction implements CellFormulaFunction<Object> {
9
10     @Override
11     public Object evaluate(CellValueVisitor visitor, AstArgList args) {
12         if (args.values.size() != 2) throw new IllegalStateException();
13         
14         Object number = args.values.get(0).accept(visitor);
15         Object digits = args.values.get(1).accept(visitor);
16         
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();
21         
22         double n = SpreadsheetGraphUtils.asNumber(number);
23         double n2 = SpreadsheetGraphUtils.asNumber(digits);
24         Double scale = Math.pow(10, n2);
25
26         long l = Math.round(scale * n);
27         Double d = Double.valueOf(((double) l) / scale);
28         return d;
29     }
30 }