conversion Char variable to var or float variable

For general Flowcode discussion that does not belong in the other sections.
Post Reply
solozerouno
Posts: 113
http://meble-kuchenne.info.pl
Joined: Tue Dec 08, 2020 4:36 am
Has thanked: 2 times
Been thanked: 2 times

conversion Char variable to var or float variable

Post by solozerouno »

```cpp
void setup() {
Serial.begin(9600); // Inizializza la comunicazione seriale
}

void loop() {
char numeroChar[] = "12,3"; // Variabile contenente il numero come caratteri
int numeroIntero = 0;
float numeroFloat = 0.0;
bool contieneVirgola = false;
int posizioneVirgola = -1;

for(int i = 0; i < strlen(numeroChar); i++) {
if(numeroChar == ',') {
contieneVirgola = true;
posizioneVirgola = i;
break;
}
}

if(contieneVirgola) {
// Conversione parte intera
for(int i = 0; i < posizioneVirgola; i++) {
numeroIntero = numeroIntero * 10 + (numeroChar - '0');
}

// Conversione parte decimale
float divisione = 0.1;
for(int i = posizioneVirgola + 1; i < strlen(numeroChar); i++) {
numeroFloat = numeroFloat + (numeroChar - '0') * divisione;
divisione = divisione * 0.1;
}

numeroFloat = numeroFloat + numeroIntero; // Somma la parte intera e decimale
Serial.print("Numero in float: ");
Serial.println(numeroFloat);
} else {
for(int i = 0; i < strlen(numeroChar); i++) {
numeroIntero = numeroIntero * 10 + (numeroChar - '0');
}
Serial.print("Numero in intero: ");
Serial.println(numeroIntero);
}

delay(1000); // Ritardo di 1 secondo tra le letture
}
```in this code, the `char` variable named `charnumber` contains the number as characters. The variable `Integer` is used to store the integer version of the number, while `FloatNumber` stores the floating-point version of the number.

The code iterates over the characters of the `charnumber` variable using a `for` loop. If the character `','` is found, the variable `containsComma` is set to `true` and the position of the comma is stored in the variable `CommaPosition`.

Based on the value of `containsComma`, the appropriate conversion is performed. If `containsComma` is `true`, conversion is performed for the integer and decimal portion, using the `'0'` character subtraction operation to obtain the numeric value of each character.

Finally, the converted value is printed to the serial port using the `println()` function.

If `CharNumber` is set to "145", the program will print "Integer Number: 145" to the serial port. If instead `CharNumber` is set to "12.3", the program will print "Number in float: 12.3".


how can I translate the above listing into flowcode, where a char variable with or without comma is translated into a var or float variable. thanks solozerouno

Post Reply