Intermittent "-" appearing in code output

Hi Everyone,

My first post but I have spent plenty of time reading here previously! I am currently reading the value from a float value from a sensor which I am then shifting two decimal places and then converting to a string. The flow is like this:

Acquire Data
Shift two decimal places
Convert from float to String
Pad with zeros to ensure a size of 7 digits

An example of this in data form would be:

135.96 (Acquired)
13596 (shifted)
13596 (string)
0013596 (padded with zeroes)

and here is the functions that do this:

// Global variables 
float ratio = 0.0;
float concentration = 0.0;

void getParticleCount()
{
  //Particle sensor Code ******************************************
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;

  if ((millis()-starttime) > sampletime_ms)
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve   
    lowpulseoccupancy = 0;
    starttime = millis();
  }    
      partString = floatToString(concentration); 

      //Pad particle string with leading zeros to ensure correct data format
      while(partString.length() != 7 && partString != "0000000")
      {
          partString = String("0"+ partString);
      }    
}


 // FUNCTION - ************ Convert float to string ***************
String floatToString (float input)
{
  //convert float to integar
  int shiftedVal = input*100.00;
   //Output wont be precisely same as original float is only an approximation    
  String stringOne = String(shiftedVal);
  return stringOne;
}

Occasionally the number goes incorrect with something like the following where the number shifted/string values bear no resemblance to the input which never goes below zero:

390.49 (Acquired)
0-26488 (shifted)
-26488 (string)
0-26488 (padded with zeroes)

I just can't figure out where it is coming from or what is causing it...
Thanks Guys

and here is the functions that do this:

That is not all that we need to see. How is concentration defined? ratio?

Once you have converted the float to an int, you could (and, really, you should) use sprintf() to convert the int to a string with 7 digits with leading zeros.

Think about what happens to 340.49 when you multiply by 100. What is the value? Does that fit into an int?

Hi Paul, I have modified the post to include the declarations for concentration and ratio.

It never even occurred to be about the limitations of using an "int" so I have changed that line to use a "long" instead which should be able to cater for any value coming in. I have re compiled the program now and am watching to see if the error re-occurs. I think you are right though, this error only seemed to occur with larger numbers.

this error only seemed to occur with larger numbers.

Yep. Any value over 327.67.

char buff[8];
sprintf(buff, "%07d", (long)concentration*100.0);

And no need to waste resources on Strings.