Go Down

### Topic: How can I have 2 decimal with a double converted to a char (Read 186 times)previous topic - next topic

#### pierrot10

##### Sep 13, 2017, 10:52 am
Hello,

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

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

Code: [Select]
`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])

Code: [Select]
`sprintf(val,"%2f",P);`

however, val print me
Quote
20.144327
I thought that %2f will limit the decimal to 2?

Many thank for your help and other suggetsion
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### pierrot10

#1
##### Sep 13, 2017, 11:00 am
By the way, it possible to convert a double to a float?
Thank a lot
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### groundFungus

#2
##### Sep 13, 2017, 12:58 pmLast Edit: Sep 13, 2017, 01:01 pm by groundFungus
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.

#### pierrot10

#3
##### Sep 13, 2017, 01:26 pm

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

Thank a lot and have a nice day
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### AWOL

#4
##### Sep 13, 2017, 01:31 pm
dtostrf
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### pierrot10

#5
##### Sep 13, 2017, 04:32 pm
Thanks!!!
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### pierrot10

#6
##### Sep 13, 2017, 04:55 pm

Code: [Select]
`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
Code: [Select]
`double value =34.5678char result[10];char *dtostrf(value, 1, 2, result)`

result ptinted me
Quote
23.56
bas as the second parameter is 1, shoud it not print me
Quote
3.56
For the second parameter, I also tried, 6 and 2 and I got the same display

Than a lot
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### groundFungus

#7
##### Sep 13, 2017, 04:57 pmLast Edit: Sep 13, 2017, 05:02 pm by groundFungus
Quote
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.

#### pierrot10

#8
##### Sep 13, 2017, 05:09 pm
Quote
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
Thank
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### groundFungus

#9
##### Sep 13, 2017, 05:16 pmLast Edit: Sep 13, 2017, 05:17 pm by groundFungus
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).

#### pierrot10

#10
##### Sep 13, 2017, 05:20 pm
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?
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!
You can not know everything, perseverance, it is almost everything!

#### sterretje

#11
##### Sep 13, 2017, 05:56 pm
As far as I know, width is the total width (including decimal dot).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

#12