Eralt / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Add capability to convert floats to Strings #372

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently can't generate a String object from a float.  Would be useful

Original issue reported on code.google.com by tom.i...@gmail.com on 8 Oct 2010 at 2:01

GoogleCodeExporter commented 9 years ago
Here's a quickie function for it based on printFloat():

String floatToString(double number, uint8_t digits) 
{ 
  String resultString = "";
  // Handle negative numbers
  if (number < 0.0)
  {
     resultString += "-";
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;

  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  resultString += int_part;

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    resultString += "."; 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    resultString += toPrint;
    remainder -= toPrint; 
  } 
  return resultString;
}

Original comment by tom.i...@gmail.com on 8 Oct 2010 at 2:16

GoogleCodeExporter commented 9 years ago
Would appreciate int and long int (time_t) to String as well, since Float's 
been Accepted.

Right now I'm stuck like this:
  sprintf(stringbuf,"unixtime: %lu", now()); 
  uoled.DrawText(0, 1, 0, stringbuf, color);

Tracking down and reading sprintf's call parameter documentation is quite... 
um. Wall-of-text feeling to newbies. @_@

Original comment by Kamil...@gmail.com on 1 Jan 2011 at 7:33

GoogleCodeExporter commented 9 years ago
I believe the best implementation for this would be to just add an option to 
add these linker arguments: -Wl,-u,vfscanf -lscanf_flt -Wl,-u,vfprintf 
-lprintf_flt
This could be archived by adding an option for custom linker arguments (as 
described in http://code.google.com/p/arduino/issues/detail?id=421) but I think 
it would be better to have the option to enable/disable this for each sketch 
independently (that would mean having something like a sketch preferences menu).

Original comment by Gregor.G...@gmail.com on 8 May 2012 at 10:38

GoogleCodeExporter commented 9 years ago
Awesome, thanks for that code. Worked a treat!

Original comment by edwardta...@gmail.com on 3 Jun 2012 at 6:28

GoogleCodeExporter commented 9 years ago
That was fixed upstream.

Original comment by c.mag...@arduino.cc on 16 Mar 2014 at 1:50

GoogleCodeExporter commented 9 years ago
works perfect.
Thanks!!

Original comment by vmibo...@gmail.com on 28 Aug 2014 at 6:44

GoogleCodeExporter commented 9 years ago
It's not that simple; although I am a newbie, I adventure to give my thinking:
A programming language must be congruent, String must work with floats just 
like print and println does.
I just encounter this issue trying to log to a SD a string with comma separated 
fields: a char tag, a int, and a float. I expected that by += them one by one 
to work, just like the println I would use to send it to the SD, but alas!, 
compiler error!.
I Know that I can get around that with tricks like the one proposed here, but 
it is what we call here a "chapuza". I will get along with my chapuza; broke 
the function to save to SD in parts and sending each field alone.

Original comment by jaimejdi...@gmail.com on 8 Oct 2014 at 12:58