# How to convert DOUBLE to char*??

#### HappyC4mper

Joined Oct 13, 2017
57
C++:
#include <mbed.h>
#include <stdio.h>

int main()
{
double Test1 = 1.234;
char* Test1_s = "DIDNT WORK";

Serial PC(USBTX, USBRX);

while(1)
{
//Convert Test1 to Test1_s

PC.printf("%s\n", Test1_s);

while(1);
}
}

Hey guys,
I have a slight problem which I cannot figure out. How do I convert 'Test1' to 'Test1_s'. Im using mbed

#### dl324

Joined Mar 30, 2015
10,072
Don't understand your question. Are you trying to print a double as a string?

sprintf or printf will do it, but some IDE's don't support floating point conversions so you have to use workarounds. Also, including sprintf in your executable is expensive. That's why some IDE's leave out support for floating point.

#### MrChips

Joined Oct 2, 2009
20,330
Test1 is double.
Test1_s is pointer, as you already know.
Why would you want to do this?

Do you mean something like this?
Test1_s = num2str(Test1);

Why not just
pc.printf("%d\r\n", Test1);

#### HappyC4mper

Joined Oct 13, 2017
57
Don't understand your question. Are you trying to print a double as a string?

sprintf or printf will do it, but some IDE's don't support floating point conversions so you have to use workarounds. Also, including sprintf in your executable is expensive. That's why some IDE's leave out support for floating point.
Test1 is double.
Test1_s is pointer, as you already know.
Why would you want to do this?

Do you mean something like this?
Test1_s = num2str(Test1);

Why not just
pc.printf("%d\r\n", Test1);
So i have a command prompt to see if it works or not.

how to make Test1_s (which is a char*) become the value of Test1 (which is a double (1.234)). Ignore "didn't work"

Basically, I want test_1 which is 1.234 to show up on test. If it doesn't work, it would show up as ''Didn't work''. But if the conversion worked, test1 should show up as 1.234.

#### MrChips

Joined Oct 2, 2009
20,330
Try:

char s[20] = "Didn't work";

sprintf(s,"%f",Test1);
pc.printf("%s\r\n", s);