How to calculate the absolute value and keep two decimal places in C language?
In C language, you can use the fabs function to calculate the absolute value and use the printf function to output with two decimal places.
The sample code is shown below:
#include <stdio.h>
#include <math.h>
int main() {
double num = -3.14159;
double abs_num = fabs(num);
printf("绝对值保留两位小数: %.2f\n", abs_num);
return 0;
}
Output result:
绝对值保留两位小数: 3.14