Skip to content

Commit

Permalink
Add presicion property into expression question fix #6556 (#6582)
Browse files Browse the repository at this point in the history
* Add presicion property into expression question fix #6556

* Add description

---------

Co-authored-by: Roman Tsukanov <roman.tsukanov@devexpress.com>
  • Loading branch information
andrewtelnov and Roman Tsukanov authored Jul 25, 2023
1 parent e11e77b commit 2ad69c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/question_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down
22 changes: 22 additions & 0 deletions tests/question_expressiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <QuestionExpressionModel>survey.getQuestionByName("q1");
const q2 = <QuestionExpressionModel>survey.getQuestionByName("q2");
const q3 = <QuestionExpressionModel>survey.getQuestionByName("q3");
const q4 = <QuestionExpressionModel>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");
});

0 comments on commit 2ad69c5

Please sign in to comment.