# So no %f for sprintf.... what's your solution? \[SOLUTION\]

**URL:** <https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411>\
**Category:** Programming\
**Created:** [July 3, 2013, 3:57pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411 "2013-07-03T15:57:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 3:57pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/1 "2013-07-03T15:57:20Z")

</div>

[FIX]  
I wrote a utility function, see below  
[/FIX]

I'm gobsmacked that %f is not supported in sprintf... (I'm not going to prove it to the person who is just about to ask me to... I have three broken teeth and a split lip so there!).

So what do you do please? I need it to conveniently (loadsa Serial.prints are a bit of a mess) format output strings to be passed to Processing and sprintf would have been ideal.

- PString looks nearly there...

- Hacking the compiler process to get AVR functionality (or whatever) to compile (if possible at all) sounds too fragile...

- dtostrf() - can't find the format characters it supports ☹

- Build your own...

Thnx

---

<div class="post-metadata">

**Author:** ![nicoverduin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nicoverduin/32/214695_2.png) [@nicoverduin](https://forum.arduino.cc/u/nicoverduin)\
**Post date:** [July 3, 2013, 4:02pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/2 "2013-07-03T16:02:48Z")

</div>

dtostrf(resultstring \*, width, decimal points, float value);

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [July 3, 2013, 4:03pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/3 "2013-07-03T16:03:31Z")

</div>

Assign the value to an int. There is your number before the decimal point.  
Multiply it by 10^n where n is the number of digits you want after the decimal point. Assign that value to an int and modulus divide by 10^n. There is your number after the decimal point. Use "%d.%d" with those two values in sprintf().

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 3, 2013, 4:15pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/4 "2013-07-03T16:15:01Z")

</div>

My solution: don't use floats in the first place.

Just work with bigger numbers.

E.g., instead of volts (2.87V), work with millivolts (2870mV). Instead of seconds (12.847s) work with milliseconds (12847ms). Etc.

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 4:19pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/5 "2013-07-03T16:19:01Z")

</div>

Lots of geometry calculations so its all floating point stuff

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 5:08pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/6 "2013-07-03T17:08:38Z")

</div>

I think you got the params wrong ☹

> dtostrf(resultstring \*, width, decimal points, float value);

Header file says

```arduino
extern char *dtostrf(double __val, signed char__ width,
                    unsigned char __prec, char *__ s);

```

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 7:27pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/7 "2013-07-03T19:27:35Z")

</div>

Here is a little piece of code I've put together to deal with the no float support in sprintf.

Arrch could you post your solution as code please??

```cpp
/*
    Utility function for use in printing floats into buffers like sprintf(...

    The library function dtostrf is the basis of this utility. It does a lot of work
    in converting floats to strings but what makes it a bit irritating is that it needs
    a buffer to write the float into which makes it very cumbersome in in-line code -
    just rewrite the sprintf example below using dtostrf!

    A collection of static char buffers is maintained in the function so that they are used
    consequetively with each call. A single buffer cannot be used when the function is used
    as a parameter to a call like sprintf in the example/test code below. This is simply because
    the compiler writes the code so that all the calls to fs2 are made before passing the
    results of the calls to the routine sprintf.

    This function does hard work of cycling through the internal buffers. The default
    number of buffers is 10 so a sprintf, for example, with more than 10 calls to fs2 will
    overwrite the the beginning of the buffer. Avoid this by making a subsequent call to sprintf.

    Please feel free to use this as you wish... I'm sure some enterprising coder will improve it :)

    Alan Boother 2013/07/03
*/

// float to string
char *f2s(float f, int p){
  char * pBuff;                        // use to remember which part of the buffer to use for dtostrf
  const int iSize = 10;                // number of bufffers, one for each float before wrapping around
  static char sBuff[iSize][20];        // space for 20 characters including NULL terminator for each float
  static int iCount = 0;                // keep a tab of next place in sBuff to use
  pBuff = sBuff[iCount];                // use this buffer
  if(iCount >= iSize -1){              // check for wrap
    iCount = 0;                        // if wrapping start again and reset
  }
  else{
    iCount++;                          // advance the counter
  }
  return dtostrf(f, 0, p, pBuff);      // call the library function
}

```

And here is some test/sample code

```cpp
// test/example code below

char sOut[200];                        // big space for output

void setup()                            // test funtion in setup
{
  float s0 = 0.1;                      // this one will be overwritten by s10
                                        // with the current buffer in f2s of 10 floats
  float s1 = 1.1;
  float s2 = 2.1;
  float s3 = 3.1;
  float s4 = 4.1;
  float s5 = 5.1;
  float s6 = 6.1;
  float s7 = 7.1;
  float s8 = 8.1;
  float s9 = 9.1;
  float s10 = 10.1;

  Serial.begin(9600);

  sprintf(sOut, "0: %s, 1:%s, 2:%s, 3:%s, 4:%s, 5:%s, 6:%s, 7:%s, 8:%s, 9:%s, 10:%s", f2s(s0, 4), f2s(s1, 4), f2s(s2, 4), f2s(s3, 4), f2s(s4, 4), f2s(s5, 4), f2s(s6, 4), f2s(s7, 4), f2s(s8, 4), f2s(s9, 4), f2s(s10, 4));
  Serial.println(sOut);
  Serial.println("The output above has overwritten 0:with 10:'s value");
  Serial.println();

  sprintf(sOut, "0: %s, 1:%s, 2:%s, 3:%s, 4:%s, 5:%s, 6:%s, 7:%s, 8:%s, 9:%s", f2s(s0, 4), f2s(s1, 4), f2s(s2, 4), f2s(s3, 4), f2s(s4, 4), f2s(s5, 4), f2s(s6, 4), f2s(s7, 4), f2s(s8, 4), f2s(s9, 4));
  Serial.println(sOut);
  Serial.println("The output above only needs 10 buffers so overwriting hasn't occured");
  Serial.println();

}

void loop(){
}

```

---

<div class="post-metadata">

**Author:** ![nicoverduin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nicoverduin/32/214695_2.png) [@nicoverduin](https://forum.arduino.cc/u/nicoverduin)\
**Post date:** [July 3, 2013, 7:53pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/8 "2013-07-03T19:53:15Z")

</div>

> acboother:  
> I think you got the params wrong ☹
> 
> > dtostrf(resultstring \*, width, decimal points, float value);
> 
> Header file says
> 
> ```arduino
> extern char *dtostrf(double __val, signed char__ width,
> 
> ```

unsigned char \_\_prec, char \*\_\_s);

> ```arduino
> 
> ```

oops 😊sorry. My mistake. Typed it wrong

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [July 3, 2013, 8:02pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/9 "2013-07-03T20:02:08Z")

</div>

> acboother:  
> Arrch could you post your solution as code please??

Really simple-

Say you start with a float value:

```arduino
float myFloat = 123.45;

```

We want to break this up into two ints: 123 and 45. Getting 123 is trivial:

```arduino
int a = myFloat; // a will contain 123

```

We can then multiple myFloat by 10^n where n is the number of digits precision. We want two digits, so we multiply it by 10^2 or 100, and store the result in an int.

```arduino
int b = myFloat * 100; // b will contain 12345

```

To extract 45 from 12345, we can use modulus division:

```arduino
b = b % 100; // 12345 % 100 = 45

```

We can then finally pass the result into sprintf:

```arduino
sprintf(buffer, "The number is %d.%d", a, b);

```

Be aware, though, that you may need to use long variables instead of ints, depending on what the maximum value you can get, and how many digits precision you want.

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 8:46pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/10 "2013-07-03T20:46:18Z")

</div>

Arrch - I thought this was you meant but it doesn't work... look at 123.045 it returns 123.4... oh dear. 😥

```cpp
void setup()
{

  char buffer[100];

  float myFloat = 123.045;

  int a = myFloat; // a will contain 123

  int b = myFloat * 100; // b will contain 12345

  b = b % 100; // 12345 % 100 = 45

  sprintf(buffer, "The number is %d.%d", a, b);
  Serial.begin(9600);
  Serial.println(buffer);
}

void loop(){
}

```

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [July 3, 2013, 8:53pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/11 "2013-07-03T20:53:21Z")

</div>

When in doubt, print it out.

If you printed b, you would see that b will contain 4, not 45. if you want it to contain 45, you need to multiply it by 10^3 or 1000. You will also want to modify your sprintf format codes to account for leading zeros. An example of such appears at the bottom of this page:

[http://www.cdf.toronto.edu/~ajr/209/notes/printf.html](http://www.cdf.toronto.edu/~ajr/209/notes/printf.html)

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 8:55pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/12 "2013-07-03T20:55:10Z")

</div>

You missed the point it should be 123.045 - the zero is missing

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 3, 2013, 8:56pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/13 "2013-07-03T20:56:18Z")

</div>

But you know it is missing, therefore you can restore it.  
%d.%d is just too simplistic.  
Why do you expect the %d on the right of the point to behave any differently to the one on the left?

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [July 3, 2013, 8:58pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/14 "2013-07-03T20:58:50Z")

</div>

> acboother:  
> You missed the point it should be 123.045 - the zero is missing

You must not have read the second sentence of my post:

> You will also want to modify your sprintf format codes to account for leading zeros. An example of such appears at the bottom of this page:
> 
> [CSC 209: C: printf and scanf](http://www.cdf.toronto.edu/~ajr/209/notes/printf.html)

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 8:59pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/15 "2013-07-03T20:59:28Z")

</div>

Agreed

and it doesn't handle negative numbers...

---

<div class="post-metadata">

**Author:** ![Arrch](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Arrch](https://forum.arduino.cc/u/Arrch)\
**Post date:** [July 3, 2013, 9:01pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/16 "2013-07-03T21:01:10Z")

</div>

> acboother:  
> Agreed
> 
> and it doesn't handle negative numbers...

Again, something that can be easily accounted for.

---

<div class="post-metadata">

**Author:** ![bperrybap](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bperrybap/32/605367_2.png) [@bperrybap](https://forum.arduino.cc/u/bperrybap)\
**Post date:** [July 3, 2013, 9:34pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/17 "2013-07-03T21:34:46Z")

</div>

> acboother:  
> I'm gobsmacked that %f is not supported in sprintf...

But it IS supported by the AVR libC.  
The issue is that that the Arduino IDE links in a version  
of sprintf() that that has floating point support explicitly removed to save code space.  
Combine that with the IDE does not let you change the linker options to allow linking in  
the version that does support floats.  
(The newer IDE used for DUE does allow modifying the linker options)

Have a read of this thread for details and how to patch your libc libraries to permanently enable  
floating point support in the xxprintf() routines:  
[http://forum.arduino.cc/index.php?topic=124809.0](http://forum.arduino.cc/index.php?topic=124809.0)

--- bill

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 3, 2013, 10:11pm UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/18 "2013-07-03T22:11:45Z")

</div>

I noticed some format stuff lurking in the core code.

It does seem unfortunate that the code to support it isn't available as something that handles floats is obviously floating about (pun intended, sorry) to support Serial.print printing floats... oh well, such are things.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [July 4, 2013, 8:18am UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/19 "2013-07-04T08:18:16Z")

</div>

> acboother:  
> I'm gobsmacked that %f is not supported in sprintf... (I'm not going to prove it to the person who is just about to ask me to... I have three broken teeth and a split lip so there!).

A lot of functionality is in the libraries, and the linker then strips it out if you don't use it. So far so good. But the %f is a runtime thing, so the linker can't strip it out, and it takes a lot of memory. Thus it was omitted.

Other people are gobsmacked that printf takes so much program memory, so you can't please everyone. 🙂

There is a "format double" library available, see this link:

[http://forum.arduino.cc/index.php?topic=85692.msg1127105#msg1127105](http://forum.arduino.cc/index.php?topic=85692.msg1127105#msg1127105)

It's not too much trouble to include that and then get your floats printed out nicely.

Plus, as I recall, Serial.print handles floats natively.

---

<div class="post-metadata">

**Author:** ![acboother](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/acboother/32/216157_2.png) [@acboother](https://forum.arduino.cc/u/acboother)\
**Post date:** [July 4, 2013, 8:33am UTC](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411/20 "2013-07-04T08:33:49Z")

</div>

I like the idea of using the sort of routine you mention for formatting in order to keep the program size down by avoiding the clutter of additional library functionality that's not needed.

I would also 'embed' the print buffers inside it at the same time, as I did in my example above, as one of the fundamental irritants is having to provide the print buffer as a parameter. Having to manage them and how long the line of code becomes is a pain and a source of typing woes, again imagine my sprintf with having to provide the print buffers as well. ☹

I have some source code for a big number library too, C compilers, linked list managers etc etc... except its on a 3.5 floppy disc... doh

[Next page](https://forum.arduino.cc/t/so-no-f-for-sprintf-whats-your-solution-solution/171411.md?page=2)
