floats to ints for temperature reading

I have this digital thermometer from Sparkfun: tmp102. I used the example code linked on the product page to get values from the thing. But I have an issue from there. I want to take the value given and print (strobe) them to a 4x7 segment display. My issue is getting from a float of some unknown length to four integers.

Let’s say the value read is 70.68. I need to get it so that:
a=7
b=0
c=7 (because it rounded up)

Or if the value is 101.56:
a=1
b=0
c=2 (because it rounds up)

The purpose of the fourth digit is to determine if the third gets rounded. But the three digits get printed to the display.

I’ve got:

//Arduino 1.0+ Only
//Arduino 1.0+ Only

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints temperature via serial
//////////////////////////////////////////////////////////////////
// SDA is on analog pin 4, and SCL is on analog pin 5

#include <Wire.h>
int tmp102Address = 0x48;
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b01111010,0b11100000,0b11111110,0b11100110}; // 0-9
const int ser = 8;
const int load = 10;
const int sck = 9;
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

void setup(){
  Wire.begin();
  pinMode(ser,OUTPUT);
  pinMode(sck,OUTPUT);
  pinMode(load,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  digitalWrite(load,0);
  shiftOut(ser,sck,LSBFIRST,0);
  digitalWrite(a1,1);
  digitalWrite(a2,1);
  digitalWrite(a3,1);
  digitalWrite(a4,1);
  digitalWrite(load,1);
}
int a;
int b;
int c;
int s;
void loop(){
  int dp;

  float celsius = getTemperature();
  
  float fahrenheit = (1.8 * celsius) + 32;  
  
//  delay(200); //just here to slow down the output. You can remove this
  
 int f=(int)(fahrenheit*10.0);
 int g=f;
  if(g/1000 != 0){
    a=g/1000;
    b=(g/100)-(a*10);
    c=(g/10)-(((a*10)+b)*10);
    if((g-((a*100)+(b*10)+c)*10) > 4){
      c++;
    dp = 0;
    }
  }
  else{
    a=g/100;
    b=(g/100)-(a*10);
    c=g-(((a*10)+b)*10);
  }
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,0b10001110);
  digitalWrite(load,1);
  digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[a]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  if(dp==0){
    s = numbers[b];
  }
  else{
    s = numbers[b]+1;
  }
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,s);  
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[c]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
//  delay(10);
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
}

Why not take a moment to explain what that program actually does, and how it differs from what you want it to do.

This might help - not that it has received much interest.

http://forum.arduino.cc//index.php?topic=172192.msg1279382#msg1279382

free-bee: Let's say the value read is 70.68. I need to get it so that: a=7 b=0 c=7 (because it rounded up)

Or if the value is 101.56: a=1 b=0 c=2 (because it rounds up)

The purpose of the fourth digit is to determine if the third gets rounded. But the three digits get printed to the display.

Do you really want it to self-scale without making it obvious that it has done? For example, with the approach you described, 10.2 and 102 would appear the same. If that's really what you want then I suggest you simply test whether the value is less than 100 degrees and multiply the float value by 10 or 100 to give you the integer value you want to display. You can simply cast it to an integer to get the significant digits to be displayed - add 0.5 first if you want the result to be rounded. Then you can either print the integer to get an ascii string, or use div/mod to extract the decimal digits from it.

PeterH:
Do you really want it to self-scale without making it obvious that it has done? For example, with the approach you described, 10.2 and 102 would appear the same. If that’s really what you want then I suggest you simply test whether the value is less than 100 degrees and multiply the float value by 10 or 100 to give you the integer value you want to display. You can simply cast it to an integer to get the significant digits to be displayed - add 0.5 first if you want the result to be rounded. Then you can either print the integer to get an ascii string, or use div/mod to extract the decimal digits from it.

Sometimes, I miss the simple things. This approach is 100x simpler than I anticipated.

And to answer jremington:
The code, as it currently sits, gets a reading from the sensor. But what gets printed to the display is:
F7x.4.
where ‘x’ is different every single time it gets written to the display, and the second decimal point may or may not show up on the write. Meanwhile, it was supposed to say something closer to:
F63.5

And it works now. It seems my analog thermometer is very wrong (it shows it’s 43 F), but I think the digital one is correct. I wrote a PHP script on my local server to verify my maths. And here is my working code:

//Arduino 1.0+ Only
//Arduino 1.0+ Only

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints temperature via serial
//////////////////////////////////////////////////////////////////
// SDA is on analog pin 4, and SCL is on analog pin 5

#include <Wire.h>
int tmp102Address = 0x48;
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9
const int ser = 8;
const int load = 10;
const int sck = 9;
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

void setup(){
  Wire.begin();
  pinMode(ser,OUTPUT);
  pinMode(sck,OUTPUT);
  pinMode(load,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  digitalWrite(load,0);
  shiftOut(ser,sck,LSBFIRST,0);
  digitalWrite(a1,1);
  digitalWrite(a2,1);
  digitalWrite(a3,1);
  digitalWrite(a4,1);
  digitalWrite(load,1);
}
int a;
int b;
int c;
int s;
void loop(){
  int f;
  boolean dp = 0;

  float celsius = getTemperature();
  
  float fahrenheit = (1.8 * celsius) + 32;  
  
//  delay(200); //just here to slow down the output. You can remove this
 if(fahrenheit >= 100){
 f=(int)(fahrenheit+0.5);
 }
 
else{
  f=(int)((fahrenheit+0.05)*10.0);
  dp=1;
}

a=f/100;
b=(f/10)-(a*10)+dp;
c=f-(a*100)-((b-dp)*10);
 
 
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,0b10001110);
  digitalWrite(load,1);
  digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[a]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
  delay(5);
  
  if(dp==0){
    s = numbers[b];
  }
  else{
    s = numbers[b]+1;
  }
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,s);  
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
  delay(5);
  
  digitalWrite(load,0);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
  shiftOut(ser,sck,LSBFIRST,numbers[c]);
  digitalWrite(load,1);
  digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
  delay(5);
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
}