cancel
Showing results for 
Search instead for 
Did you mean: 

float to char buffer in c using SPRINTF not work.

parth kothiya
Senior

I want to display floating point number on 8digit 7segment display MAX7219 so i want to convert float value in to char buffer using sprintf() but it not gives exact output

True Studio  STM32F103

void display_charDigit(float Value){
	char buffer[8];
	sprintf(buffer,"%2.3f",Value);
	for (uint8_t digit = 6; digit > 0; digit--) {
		write_char((7-digit),buffer[6-digit],false);
	}
}
 
while(){
display_charDigit(12.345);
}

2 REPLIES 2
Piranha
Chief II

https://community.st.com/s/question/0D50X0000B0AEJb/uprintffloat-where-to-set-for-clinker-in-stm32cubeide

Also use snprintf() instead. And you don't have to count backwards to write those characters.

Ozone
Lead

> sprintf(buffer,"%2.3f",Value);

 It seems you did not understand the printf() format string rules.

The width field defines the overall length, not an integral part. Akin to "%8.3f".

Your string is incorrect, as the precision field is defined larger than the overall length. 

> display_charDigit(12.345);

As a side note, this is not a float number.