Convert an int to char array, then append another char

Hi,
I'm trying to convert an int to a char array then append another char on the end. This is to be outputted to an LCD. I keep getting stuck when trying to follow examples on the web.

long number = x;   //value from user input 0 to 2million
char buf[12];         // Output buffer
char suffix[2];       // suffix for appending to number (k or M)
int thousands = (number/1000);
suffix[0] = 'k';
itoa(reducedNo, buf, 10);   // convert number to text
buf+=suffix; //invalid operands to binary operator+

My brain has melted now. Does anyone have a good way to do this without using String?

How about using sprintf() ?

long number;
char buffer[12];

void setup() 
{
  Serial.begin(115200);
  
  number = 2000000;
  sprintf(buffer,"%luk",number);
  Serial.println(buffer);

  number = 1;
  sprintf(buffer,"%luk",number);
  Serial.println(buffer);
}

void loop() 
{
}
buf+=suffix; //invalid operands to binary operator+

char arrays can't be concatenated with the + operator. You can use strcat(), although you will need to ensure suffix is a valid c-style string (Needs to be null-terminated, you don't explicitly make it). You can also use sprintf like UKHeliBob suggested, or you can simply do this:

Get the length of the string:

int len = strlen(buf);

"Append" 'k' to the string by replacing the null with your character

buf[len] = 'k';

And setting the next character to be null:

buf[len+1] = '\0;

Thanks. Sorry I missed something quite important! :blush:
There should be a line..

float reducedNo = number/1000

And i should have been asking how to convert a float! [facepalm]

So this means sprintf wont work as it does not like floats on the arduino.
itoa also wants an int

Basically what I am trying to do is display a frequency on an LCD that can be anything from 0 to 2 million. I'm hoping to make the larger numbers easier to read by making them look like 10k instead of 10000.

It may be just as easy and efficient to just print to the LCD and use a conditionals to select a different format depending on the size of the number.

Something like

if number > 999999 use 1M or 2M
if number > 999 use 1K or 25K
otherwise it's less than 4 digits just output the number

You'll likely need something similar for formatting anyway.

dtostrf() is a function that works well on Arduino for converting floats to string. Might be worth looking up.

RichMo:
Basically what I am trying to do is display a frequency on an LCD that can be anything from 0 to 2 million. I'm hoping to make the larger numbers easier to read by making them look like 10k instead of 10000.

I think what you're looking for is dtostrf()

Thanks guys. I've used a combination of these in my code to get everything displayed nice.