Newbe needing help with fixed string length im trying to build a bit of test equipment that will talk to some software and it needs to send a fix string. when the voltage changes the string length does as well. i need to cap the results to 4 in length. (sorry for not knowing the correct terminology. this is the output i need -

Where,

X = Flag CP Value
_ = Space
P = Polarity
C = CP Received value
Y = Flag Field Gradient
F = Field Gradient Received value
Z = Flag Contact CP
T = Contact CP Received value


There is also a line feed and carriage return present at the end of each string.
The polarity indicator will only display a minus sign ‘-‘ if the received value is
negative otherwise no assignation will be transmitted. Although the overall string
length is constant the values for each measurement are right justified and hence a
space will be present should the value of the measurement not necessitate the
utilisation of all allocated character spaces.
Sample Outputs

&lt; X -89 Y -1989 Z -1096 &gt;
&lt; X 1039 Y 8 Z 78 &gt;


this is my code so far

#include &lt;Wire.h&gt;
#include &lt;Adafruit_ADS1015.h&gt;

Adafruit_ADS1115 ads1(0x48);     // Use this for the 16-bit version
Adafruit_ADS1115 ads2(0x49);

void setup(void) {

  Serial.begin(9600);

  ads1.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      
  ads2.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      

  ads1.begin();
  ads2.begin();
}

void loop(void) {

  int16_t results01,results02,results03;

  results01 = ads1.readADC_Differential_0_1();
  results02 = ads1.readADC_Differential_2_3();
  results03 = ads2.readADC_Differential_0_1();

  Serial.print("X "); 
  Serial.print(round(results01 * multiplier));

  Serial.print(" Y "); 
  Serial.print(round(results02 * multiplier)); 

  Serial.print(" Z "); 
  Serial.print(round(results03 * multiplier));

  Serial.println();              // send a cr/lf
  delay(500);
}


Any help pointing me in the right direction would be Amazing.
Thanks in advance

Joe
The printf() family of functions comes handy when you need
this kind of control over the formatted output:

/*
 * Format a number as 5 characters.
 * Returns the string in a statically allocated buffer.
 */
char * format5(int x)
{
    static char buffer[6];  // 5 chars + terminating NUL
    snprintf(buffer, sizeof buffer, "%5d", x);
    return buffer;
}

void setup() {
    Serial.begin(9600);
    Serial.println(format5(15));
    Serial.println(format5(-15));
    Serial.println(format5(-1500));
    Serial.println(format5(1500));
}

void loop(){}


output:

   15
  -15
-1500
 1500