This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28379D: truncating float32 to required decimal places

Part Number: TMS320F28379D

Hello Experts ,

I would like to truncate a float32 variable to its 2 decimal places, Currently I am doing the below, Could you please let me know if there is a better way, I would like to avoid any division operation.

#include <math.h>
//Round to 2 decimal places

//Option 1:
float32_t currentMechTheta = 0.3278;
currentMechTheta = floorf(pMotor->ptrFCL->qep.MechTheta * 100) / 100;   //0.3200

//Option 2:
float32_t currentMechTheta = 0.3278;
currentMechTheta = roundf(pMotor->ptrFCL->qep.MechTheta * 100) / 100;   //0.3300

  • Hello Arun,

    Yes, this looks to be correct for truncation. Because floating point variables are not stored the same as integers, this would likely be the best way rather than manipulation individual bits in the variable.

    Best regards,

    Omer Amir