I am trying to switch for example: input 54321.987, then 4 and 2 should switch, so output would be 52341.987. 54321.777 should become 52341.777. If it is 2345.777 it should
Using string manipulation:
char string[20];
snprintf(string, sizeof(string), "%09.3f", x);
char *dot = strchr(string, '.');
assert(dot != 0 && dot > string + 4);
char old_4 = dot[-4];
char old_2 = dot[-2];
dot[-2] = old_4;
dot[-4] = old_2;
/* If you need a float back */
sscanf(string, "%lf", &x);
Using arithmetic manipulation:
double frac_part;
double int_part;
frac_part = modf(x, &int_part);
long value = int_part;
int dig_2 = (int_part / 10) % 10;
int dig_4 = (int_part / 1000) % 1000;
assert(dig_4 != 0);
value -= 10 * dig_2 + 1000 * dig_4;
value += 10 * dig_4 + 1000 * dig_2;
int_part = value;
x = int_part + frac_part;
Neither sequence of operations is minimal, but they are fairly straight-forward.