-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat2string 2dp.c
23 lines (18 loc) · 1.68 KB
/
float2string 2dp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define FLOATSTRINGSIZE 10U // Buffer size for the function Float2String
// Convert a floating point to a string, with 2 decimal digits
// Receive the floating variable and the string to store result
void Float2String(float f_Variable, char *cResult)
{
double d_IntDigits; // Integer digits
double d_FraDigits; // Fraction digits
d_FraDigits = modf((double)(fabs((double)f_Variable) + 0.005), &d_IntDigits); // Absolute and Round the value
if((f_Variable < 0.0) && (((uint16_t)d_IntDigits != 0) || ((uint8_t)(d_FraDigits * 100.0) != 0))) { // test for negative values and not zero // test for negative numbers
snprintf(cResult, FLOATSTRINGSIZE,"-%d.%02d", (uint16_t)d_IntDigits, (uint8_t)(d_FraDigits * 100.0)); // Convert float to string with 2 decimal digits, for negative values
} else {
if((((uint16_t)d_IntDigits == 0) && ((uint8_t)(d_FraDigits * 100.0) == 0))) { // test for zero values
snprintf(cResult, FLOATSTRINGSIZE,"0"); // Convert float to string for zero values, 0 instead of 0.00
} else {
snprintf(cResult, FLOATSTRINGSIZE,"%d.%02d", (uint16_t)d_IntDigits, (uint8_t)(d_FraDigits * 100.0)); // Convert float to string with 2 decimal digits, for positive values
}
}
}