X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FRoundFormulaFunction.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FRoundFormulaFunction.java;h=b350bbc035a2d4924a0595044ea32703b7639103;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=fd42c75abec869124a8b1e5022d5895a8b9943df;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/RoundFormulaFunction.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/RoundFormulaFunction.java index fd42c75ab..b350bbc03 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/RoundFormulaFunction.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/RoundFormulaFunction.java @@ -1,30 +1,30 @@ -package org.simantics.spreadsheet.graph.formula; - -import org.simantics.spreadsheet.graph.CellFormulaFunction; -import org.simantics.spreadsheet.graph.CellValueVisitor; -import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils; -import org.simantics.spreadsheet.graph.parser.ast.AstArgList; - -public class RoundFormulaFunction implements CellFormulaFunction { - - @Override - public Object evaluate(CellValueVisitor visitor, AstArgList args) { - if (args.values.size() != 2) throw new IllegalStateException(); - - Object number = args.values.get(0).accept(visitor); - Object digits = args.values.get(1).accept(visitor); - - FormulaError2 err1 = FormulaError2.forObject(number); - if(err1!=null) return err1.getString(); - FormulaError2 err2 = FormulaError2.forObject(digits); - if(err2!=null) return err2.getString(); - - double n = SpreadsheetGraphUtils.asNumber(number); - double n2 = SpreadsheetGraphUtils.asNumber(digits); - Double scale = Math.pow(10, n2); - - long l = Math.round(scale * n); - Double d = Double.valueOf(((double) l) / scale); - return d; - } +package org.simantics.spreadsheet.graph.formula; + +import org.simantics.spreadsheet.graph.CellFormulaFunction; +import org.simantics.spreadsheet.graph.CellValueVisitor; +import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils; +import org.simantics.spreadsheet.graph.parser.ast.AstArgList; + +public class RoundFormulaFunction implements CellFormulaFunction { + + @Override + public Object evaluate(CellValueVisitor visitor, AstArgList args) { + if (args.values.size() != 2) throw new IllegalStateException(); + + Object number = args.values.get(0).accept(visitor); + Object digits = args.values.get(1).accept(visitor); + + FormulaError2 err1 = FormulaError2.forObject(number); + if(err1!=null) return err1.getString(); + FormulaError2 err2 = FormulaError2.forObject(digits); + if(err2!=null) return err2.getString(); + + double n = SpreadsheetGraphUtils.asNumber(number); + double n2 = SpreadsheetGraphUtils.asNumber(digits); + Double scale = Math.pow(10, n2); + + long l = Math.round(scale * n); + Double d = Double.valueOf(((double) l) / scale); + return d; + } } \ No newline at end of file