Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

TempToAscii

Takes a floating or double floating point number in, returns an ascii string with up to 1/1000 of precision. Good for turning temperatures into a usable string.

Code:

//Matt G.
//onion@doorcreekorchard.com
//July 2008

char* tempToAscii(double temp)
{
  char ascii[32];
  int frac;
  frac=(unsigned int)(temp*1000)%1000;  //get three numbers to the right of the deciaml point

  itoa((int)temp,ascii,10);
  strcat(ascii,".");
  itoa(frac,&ascii[strlen(ascii)],10); //put the frac after the deciaml

  return ascii;
}

Example usage:

#include <OneWire.h>

byte sensor1[8]= {0x28,0xB5,0x4E,0x8A,0x01,0x00,0x00,0xD0};
OneWire  ds(12);  // on pin 12

void setup(void) {
  // initialize inputs/outputs
  // start serial port
  Serial.begin(19200);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}

void loop(void) {
  double temp;

  if (getOneWireTemp(sensor1,&temp,&ds))
  {
   Serial.println(tempToAscii(temp));
  }
}