VARIABLE TYPE CONVERTION PROBLEM

I AM GETTING SOME DATA FROM TEMPERATURE SENSOR.

IT IS A DOUBLE DATA TYPE. IS IT POSSIBLE TO CHANGE IT TO LONG OR INT DATA. AND JUST KEEP THE INTEGER PARTS.

THX

Parents
  • 1. Disable your CAPS LOCK!
    2. Yes. If you really get the value as a Keil-supported floating point value then you should need only cast the float type to an int type. E.g.

    float        tempSensorValue = readTempSensor() /* ??? */;
    int          integer         = (int) tempSensorValue;
    unsigned int fraction        = (unsigned int) (1000 * (tempSensorValue - integer));
    
    printf("Result is %d.%u\n", integer, fraction);
    What temperature sensor gives you floating point values? Do you really mean an ASCII string that looks like a floating point number?

Reply
  • 1. Disable your CAPS LOCK!
    2. Yes. If you really get the value as a Keil-supported floating point value then you should need only cast the float type to an int type. E.g.

    float        tempSensorValue = readTempSensor() /* ??? */;
    int          integer         = (int) tempSensorValue;
    unsigned int fraction        = (unsigned int) (1000 * (tempSensorValue - integer));
    
    printf("Result is %d.%u\n", integer, fraction);
    What temperature sensor gives you floating point values? Do you really mean an ASCII string that looks like a floating point number?

Children
No data
More questions in this forum