I want to round a decimal number (x), but I need to do it differently depending on how it is. If it has integer significant digits, we round to the nearest integer, and if not, we round to the first significant digit. So the following would happen with these example numbers: 11.871->12, 11.177->11, 37.233->37, 37.779->38, 0.578->0.6, 0.544->0.5, 0.0257->0.03, 0.0223->0.02... (The numbers could have more or less decimal places than in the examples).
To implement this, I have created a function (type) in which we check if the most significant digit is of type integer or decimal. This function returns this characteristic of the number in the form of a "string". So if the most significant digit is an integer we just use the "round()" function. However, I don't know how to round a number to the first significant digit (roundto_fsd() function). Does anyone know of any function capable of doing this or any method to do it?
float x;
char type(float number) {
char type[8];
if (int(number) != 0) {
type = "integer";
} else {
type = "decimal";
}
return type;
}
float roundto_fsd(float number) {}
void setup() {
Serial.begin(9600);
if (!type(x).compareTo("integer")) {
Serial.println(round(x));
} else if (!type(x).compareTo("decimal")) {
Serial.println(roundto_fsd(x));
}
}
void loop() {}