]> gerrit.simantics Code Review - simantics/platform.git/blob
20c057a8952bebf086ea7f07ee1a8924ee782aae
[simantics/platform.git] /
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 RoundupFormulaFunction 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         
25         Double scale = Math.pow(10, n2);
26
27         double l = Math.ceil(scale * n);
28         Double d = Double.valueOf(l / scale);
29         return d;
30     }
31 }