How to partition a double

This is really frustrating. In c# its a breeze to convert a double to a string.

Ive tried sprintf, snprintf, sscanf in arduino and cant get this to work. Ive tried using sprintf in Bloodshed c++ and it works fine. I basically need to get the individual digits so I can display on a set of 3 LED. Heres some code.

double sensorvalue = 123.456;

char buffer [6];

sprintf (buffer,"%1f", sensorvalue);

//Display first digit
setleddigit (datapin1, clockpin1, (int) buffer [0]);

//repeat for other two digits

The value I get in buffer [0] is ? Instead of 1

support for floats and doubles with sprintf was left out in Arduino because the code to do it took up too much room. Hence the question mark.

Instead, try dtostrf.

setleddigit (datapin1, clockpin1, (int) buffer

);

Doing (int)buffer does not convert the string or any part of it to an int.

Mark

In c# its a breeze to convert a double to a string.

Your mission Jim, should you choose to accept it, is to port C# to the AVR architecture.
This post will self-destruct in five seconds (ish)

Thanks. Decided to write up my own method instead using if and else if whilst dividing by 10 and subtracting to extract out the digits. Was a lot faster.

double v= 113.45; // Values dont exceed 360

int a, b, c;

if (v > 100.0){

a = (int) v/100;
b= (int)(v-a100)/10;
c= (int)(v -(a
100+b*10));

setleddigit (datapin1, clockpin1, a);
setleddigit (datapin2, clockpin2, b);
setleddigit (datapin3, clockpin3, c);

}

else if (v < 100 && v >=10)
{

}

etc

And floating-point division is faster on the Arduino than long integer division.

If this is fast enough for your purposes, then don't change it.

Your mission Jim, . . . . .

Smart as a whip tonight :wink: