How can I have 2 decimal with a double converted to a char

Hello,

I have again a such problem and no way to solve it :o :o

I am getting value into a double variable and I have to save it into a char variable to be sent.

The double sourve value has two digit

20.144327

and I need to have the same value to a char...

I found that soltuion:
(P is a double with the value of 20.14 and val is char value[20])

sprintf(val,"%2f",P);

however, val print me

20.144327

I thought that %2f will limit the decimal to 2?

Many thank for your help and other suggetsion

By the way, it possible to convert a double to a float?
Thank a lot

The format specifier for 2 digits (minimum) before and 2 after the decimal is %2.2f. I am surprised that sprintf works, for you, with floats. Support for floats is disabled in all the versions of the IDE that I have.
Format specifiers.

Thank for your reply!

So how you you suggest me to better convert a double to a char?

Thank a lot and have a nice day

dtostrf

Thanks!!!

Just a last question about

char *dtostrf(double value, int width, unsigned int precision, char *result)

waht is it for, the second parameter width.

Is the number before de . or the length including the decimal 24.34

I tried the following

double value =34.5678
char result[10];
char *dtostrf(value, 1, 2, result)

result ptinted me

23.56

bas as the second parameter is 1, shoud it not print me

3.56

For the second parameter, I also tried, 6 and 2 and I got the same display

Than a lot

waht is it for, the second parameter width.

That is the minimum number of characters to the left of the decimal point. If fewer characters than that number the output will be padded with spaces. Refer to the format specifier link that I posted before. Those specifiers work for dtostrf as well as print.

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

So this paameter should be alway larger in order to have a correct print. Bit I am still in the trouble, because before I put 1 but my print was 24.56

If it is the minimum number of characters to be printed, my print should be 4.56. Isn't

Sorry, not clear yet :slight_smile:
Thank

If you have 3.14 and use the width specifier 2 what will print is 'space' 3.14 (padded with a space to be 2 characters). If you have 10001334.14 and a width specifier of 2, 10001334.14 will be printed (no space character). So if width is 4 and you have 3.14, three space characters will be added in front of the 3 to make up 4 characters (minimum).

Ok, then I understand. But finnaly I should keep always the wifth parameter to 1?
In that case, all of my digit will print and no chance to have space caracter?

As far as I know, width is the total width (including decimal dot).

From the link that I posted in reply #3.

Width Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.