Skip to content

Commit 85ef06d

Browse files
InterLinked1kharwell
authored andcommitted
func_math: Return integer instead of float if possible
The MIN, MAX, and ABS functions all support float arguments, but currently return floats even if the arguments are all integers and the response is a whole number, in which case the user is likely expecting an integer. This casts the float to an integer before printing into the response buffer if possible. ASTERISK-29495 Change-Id: I902d29eacf3ecd0f8a6a5e433c97f0421d205488
1 parent 5c9d7a0 commit 85ef06d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

funcs/func_math.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ static int acf_min_exec(struct ast_channel *chan, const char *cmd,
525525
}
526526

527527
ast_debug(1, "%f is the minimum of [%f,%f]\n", response_num, num1, num2);
528-
snprintf(buffer, buflen, "%f", response_num);
528+
if ((int) response_num == response_num) {
529+
snprintf(buffer, buflen, "%d", (int) response_num);
530+
} else {
531+
snprintf(buffer, buflen, "%f", response_num);
532+
}
529533

530534
return 0;
531535
}
@@ -567,7 +571,11 @@ static int acf_max_exec(struct ast_channel *chan, const char *cmd,
567571
}
568572

569573
ast_debug(1, "%f is the maximum of [%f,%f]\n", response_num, num1, num2);
570-
snprintf(buffer, buflen, "%f", response_num);
574+
if ((int) response_num == response_num) {
575+
snprintf(buffer, buflen, "%d", (int) response_num);
576+
} else {
577+
snprintf(buffer, buflen, "%f", response_num);
578+
}
571579

572580
return 0;
573581
}
@@ -589,7 +597,11 @@ static int acf_abs_exec(struct ast_channel *chan, const char *cmd,
589597

590598
response_num = fabs(num1);
591599
ast_debug(1, "%f is the absolute value of %f\n", response_num, num1);
592-
snprintf(buffer, buflen, "%f", response_num);
600+
if ((int) response_num == response_num) {
601+
snprintf(buffer, buflen, "%d", (int) response_num);
602+
} else {
603+
snprintf(buffer, buflen, "%f", response_num);
604+
}
593605

594606
return 0;
595607
}

0 commit comments

Comments
 (0)