Using floats with sprintf() in embedded C

后端 未结 13 1877
别跟我提以往
别跟我提以往 2020-12-24 06:35

Guys, I want to know if float variables can be used in sprintf() function.

Like, if we write:

sprintf(str,\"adc_read = %d \         


        
13条回答
  •  星月不相逢
    2020-12-24 07:02

    Don't do this; integers in C/C++ are always rounded down so there is no need to use the floor function.

    char str[100]; 
    int d1 = value;
    

    Better to use

    int d1 = (int)(floor(value));
    

    Then you won't get rounding up of the integer part (68.9999999999999999 becomes 69.00..). 68.09999847 instead of 68.1 is difficult to avoid - any floating point format has limited precision.

提交回复
热议问题