Add comma in string

hi..
sorry for my english..
i have project with read serial from arduino
example : the incoming data string 12345678, and i want the data to be 123456,78.
so how i can add comma at string in 2 last digit.
thank you..

Are you receiving this data over a SUART Port? If yes, then wait until 8 characters are accumulated in the buffer. After that read 6 characters (one-by-one), save/print them and then insert a comma (,) and then read, save/print the last two characters. The sample codes:

int i;
byte n = SUART.available ();
if(n == 8)
{
     for(i = 0; i < 6; i++)
     {
         myData[i] = SUAT.read();
         Serial.print(myData[i]);
     }
     myData[i++] = ',';
     Serial.print(',');
     //--- perform two more read, save, print 
     // operations
}

Convert the string into a long and divide by 100.

Sorry. A true double would be needed.

Is it comma or period? (123456,78)

thx for reply..
sorry i mean 123456.78
so the incoming serial always different length.
so i just want to add (.) in before 2 last digit

thx for reply bro.
i haved try this method but if divide 100 the result is not same because round.

Corrected/edited the reply.

maybe something like this..


//buffer for formatted string..
char result[80];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  char* c = "123\n";
  Serial.println(c);

  AddDecimal(c);
  Serial.println(result);


}

void AddDecimal(const char* inStr) {
  int len = strlen(inStr);
  int pos = len - 3;
  memset(result,0,sizeof(result));//empty//
  if (len<3) return;//nothing to do..
  for (int i = 0; i < len; i++) {
    if (i < pos) {
      result[i] = inStr[i];
    } else {
      if (i == pos) {
        result[i] = '.';
        result[i + 1] = inStr[i];
      } else {
        result[i + 1] = inStr[i];
      }
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);

}

doesn't care about data just adds the "." to it..

good luck.. ~q

divide by 100
print the comma
print % 100

You would get better advices if you just make a short demo sketch which shows how you receive the data currently and what you want to do with that data.
A full sketch with globals, a setup and loop.

The algorithm needs to start with the end of the array of characters and then back up two characters.

As a result, in order to know where and when to insert the '.', the algorithm must read all the characters first.
Simply checking for the position index being equal to (known length -2) and inserting the '.' could solve it
but would mean checking the position and doing the math every time the serial port is read by the code.

Alternatively:

Borrowing from GolanMostafa's tidy example, with modifications.

int i;
int n = (int) SUART.available ();     // I live without classes, not sure of type returned, but want to work with ints

for(i = 0; i < n; i++)
{
    myData[i] = SUAT.read();
}

// when the serial input read completes, i and n have the same value (i is the current position, n is one past last).
// my rule is that you have to send a string with at least 3 digits before I will insert a '.' into the middle of it.
// when moving the elements of an array away from [0], you must move the each [element] to [element+1] first.
// my buffer for this example is infinite length but we still care that our indices reach valid bytes only.
// when i is 2, two characters have been entered.  logic similar to this could also insert a '0' in front of these cases.

if (i >= 3)
{
    myData[i+0] = myData[i-1];                        // copy [i-1] before [i-2] overwrites it
    myData[i-1] = myData[i-2];                         // copy [i-2] before '.' overwrites it
    myData[i-2]  = '.';                                          // insert the decimal point
    n++;
}

// once again deal with the serial port in a very focused manner
for(i = 0; i < n; i++)
{
    Serial.print(myData[i]);
}

ShockedOldDad

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.