diff --git a/src/question_expression.ts b/src/question_expression.ts index 7f56cbb55c..515312075d 100644 --- a/src/question_expression.ts +++ b/src/question_expression.ts @@ -74,7 +74,7 @@ export class QuestionExpressionModel extends Question { this.expressionRunner = new ExpressionRunner(this.expression); } this.expressionRunner.onRunComplete = (newValue) => { - this.value = newValue; + this.value = this.roundValue(newValue); this.unlocCalculation(); }; this.expressionRunner.run(values, properties); @@ -91,6 +91,7 @@ export class QuestionExpressionModel extends Question { * Default value: -1 * @see displayStyle * @see minimumFractionDigits + * @see precision */ public get maximumFractionDigits(): number { return this.getPropertyValue("maximumFractionDigits"); @@ -195,6 +196,23 @@ export class QuestionExpressionModel extends Question { public set useGrouping(val: boolean) { this.setPropertyValue("useGrouping", val); } + /** + * Specifies how many decimal digits to keep in the expression value. + * + * Default value: -1 (unlimited) + * @see maximumFractionDigits + */ + public get precision(): number { + return this.getPropertyValue("precision"); + } + public set precision(val: number) { + this.setPropertyValue("precision", val); + } + private roundValue(val: any): any { + if(this.precision < 0) return val; + if(!Helpers.isNumber(val)) return val; + return parseFloat(val.toFixed(this.precision)); + } protected getValueAsStr(val: any): string { if (this.displayStyle == "date") { var d = new Date(val); @@ -423,6 +441,7 @@ Serializer.addClass( { name: "maximumFractionDigits:number", default: -1 }, { name: "minimumFractionDigits:number", default: -1 }, { name: "useGrouping:boolean", default: true }, + { name: "precision:number", default: -1, category: "data" }, { name: "enableIf", visible: false }, { name: "isRequired", visible: false }, { name: "readOnly", visible: false }, diff --git a/tests/question_expressiontests.ts b/tests/question_expressiontests.ts index dfeacb1a7f..c9cbd9b4c7 100644 --- a/tests/question_expressiontests.ts +++ b/tests/question_expressiontests.ts @@ -172,3 +172,25 @@ QUnit.test("setting data doesn't calculate expressions survey.questionsOnPageMod assert.equal(question.displayValue, "3", "display value is correct"); assert.equal(question.formatedValue, "3", "formatedValue is correct"); }); +QUnit.test("round to digits", function (assert) { + const survey = new SurveyModel({ + questions: [ + { "name": "q1", "type": "expression", "expression": "1111/125" }, + { "name": "q2", "type": "expression", "expression": "1111/125", "precision": 2 }, + { "name": "q3", "type": "expression", "expression": "1111/125", "precision": 1 }, + { "name": "q4", "type": "expression", "expression": "1111/125", "precision": 0 } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + const q3 = survey.getQuestionByName("q3"); + const q4 = survey.getQuestionByName("q4"); + assert.equal(q1.precision, -1, "precision:-1"); + assert.equal(q2.precision, 2, "precision:2"); + assert.equal(q3.precision, 1, "precision:1"); + assert.equal(q4.precision, 0, "precision:0"); + assert.equal(q1.value, 8.888, "precision - default (-1)"); + assert.equal(q2.value, 8.89, "precision - 2"); + assert.equal(q3.value, 8.9, "precision - 1"); + assert.equal(q4.value, 9, "precision - 0"); +});