# Sprintf() a float number

**URL:** <https://forum.arduino.cc/t/sprintf-a-float-number/1013193>\
**Category:** Programming\
**Created:** [July 17, 2022, 4:22am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193 "2022-07-17T04:22:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ALAA72](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@ALAA72](https://forum.arduino.cc/u/ALAA72)\
**Post date:** [July 17, 2022, 4:22am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/1 "2022-07-17T04:22:43Z")

</div>

hi there  
I have a float number, say 6.38. I want to show that on a screen with one floating point.  
so I used the code below

```cpp
char str_tmp[5];
	sprintf(str_tmp,"%2.1f",6.38); 

```

I expect to get 6.3 but looks like the code line above, rounds up 6.38 to 6.4 and then shows it with one floating point...

how can I get 6.3 and not 6.4 ? I dont want the foating number to round up...

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 17, 2022, 4:45am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/2 "2022-07-17T04:45:40Z")

</div>

Try truncating the value

[https://m.cplusplus.com/reference/cmath/trunc/](https://m.cplusplus.com/reference/cmath/trunc/)

---

<div class="post-metadata">

**Author:** ![ALAA72](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@ALAA72](https://forum.arduino.cc/u/ALAA72)\
**Post date:** [July 17, 2022, 5:08am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/3 "2022-07-17T05:08:54Z")

</div>

that's not really what I want... any other suggestions?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 17, 2022, 5:18am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/4 "2022-07-17T05:18:15Z")

</div>

Sorry missed the point

One idea is x10, truncate and divide by 10 to get 1 decimal digit (give or take the poor precision of floats)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 17, 2022, 5:41am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/5 "2022-07-17T05:41:21Z")

</div>

For many of the Arduino processors, floats are disabled in sprintf.

Another way is to convert the float number to a string with [the dtostrf() function](https://www.programmingelectronics.com/dtostrf/) then use the %s placeholder to put it into your snprintf() function. Note that [s **n** printf()](https://cplusplus.com/reference/cstdio/snprintf/?kw=snprintf) is safer to use.

```cpp
float floatNumber = 3.141592;

void setup()
{
   Serial.begin(115200);
   char buffer[30]; // final string
   char floatStr[10]; // float converted to string
   dtostrf(floatNumber, 8, 6, floatStr); // 8 char min total width, 6 after decimal
   snprintf(buffer, sizeof(buffer), "the float number is %s", floatStr);
   Serial.println(buffer);
}

void loop()
{
  
}

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 6:40am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/6 "2022-07-17T06:40:06Z")

</div>

Your sketch shows:

```cpp
the float number is 3.141592

```

OP wants: 3.1

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [July 17, 2022, 6:40am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/7 "2022-07-17T06:40:31Z")

</div>

> [@ALAA72](#):
>
> I have a float number, say 6.38. I want to show that on a screen with one floating point.

Many arduino print functions similar to Serial.print() has the second parameter, controlling the number of floating point

```arduino
float f = 6.38;
Serial.print(f,1);

```

output the 6.4

> [@ALAA72](#):
>
> how can I get 6.3 and not 6.4 ? I dont want the foating number to round up...

do before printing:

```arduino
float f = 6.38;
int i = f * 10; // => 63
float f_1 = i/10; // => 6.3

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 9:22am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/11 "2022-07-17T09:22:18Z")

</div>

> [@groundFungus](#):
>
> I saw that the OP was using sprintf (and its in the title) so assumed that that is what they wanted to do. If all they really want is to display 6.3 from 6.38 they could just:  
> `Serial.println(6.38, 1);`

That command shows: 6.4 and not 6.3!  
**Output:**

```arduino
6.4

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 17, 2022, 10:11am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/12 "2022-07-17T10:11:45Z")

</div>

one approach

```cpp
     123.456

```

```cpp
#define frac(x) (int(1000*(x - int(x))))

char s [80];

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    float x = 123.456;

    sprintf (s, " %6d.%03d", int(x), frac(x));
    Serial.println (s);
}

// -----------------------------------------------------------------------------
void
loop ()
{
}

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 10:30am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/13 "2022-07-17T10:30:10Z")

</div>

Your output is:

```cpp
 123.456

```

But, OP wants:

```cpp
123.4

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 17, 2022, 11:10am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/14 "2022-07-17T11:10:03Z")

</div>

[

> [@GolamMostafa](#):
>
> But, OP wants:
> 
> ```arduino
> 123.4
> 
> ```

quote="gcjr, post:12, topic:1013193"]  
`#define frac(x) (int(1000*(x - int(x))))`  
[/quote]

shouldn't he be able to figure out changing 1000 to 10?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 17, 2022, 11:27am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/15 "2022-07-17T11:27:20Z")

</div>

> [@gcjr](#):
>
> shouldn't he be able to figure out changing 1000 to 10?

Esp. With hint in post #4 🙂

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 11:50am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/16 "2022-07-17T11:50:43Z")

</div>

> [@J-M-L](#):
>
> One idea is x10, truncate and divide by 10 to get 1 decimal digit (give or take the poor precision of floats)

===\>  
One idea is x10, truncate and divide by **10.0** to get 1 decimal digit (give or take the poor precision of floats)

```arduino
float n = 6.38;
==> n = n*10; //n = 63.8
==> n1 = (int)n; //n = 63
==> n = n1/10.0; //n = 6.3 n = n1/10 = 6.0

```

```arduino
void setup()
{
  Serial.begin(115200);
  float n = 6.38;
  n = n * 10; //n = 63.8
  int n1 = (int)n; //n = 63
  //Serial.println(n1); //n1 = 63
  n = n1 / 10.0; //n = 6.3
  Serial.println(n, 1);
}

void loop()
{

}

```

**Output:**

```arduino
6.3

```

So simple and that's why it is difficult for the OP?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 11:58am UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/17 "2022-07-17T11:58:32Z")

</div>

Another version found in net and then moderated. Good as an exercise as the solution contains so many fundamental concepts of C Programming.

```cpp
float n = 6.38;
char myData[10] = {0};

void setup()
{
  Serial.begin(9600);
  //--extract integer part------------
  int iPart = (int)n; // ipart = 6
  
  //--extract fractional part---------
  float fPart = n - (float)iPart; //0.38
  
  //-convert iPart into string and save in myData[]-------
  int index = intToStr(iPart, myData);//
  
  //-------place point at place marked by index-----------
  myData[index] = '.'; //place point
  
  //---- convert fPart to 3.8)----------
  fPart = fPart * 10.0;//fPart = 3.80; 
  
  //-----extract 3 from 3.8----------------
  int iPartF = (int)fPart; //iPrtF = 3
  
  //--cnvert iPartF into string and append at end og myData----- 
  intToStr(iPartF, myData + index + 1); //
  Serial.print(myData);
}

void loop()
{

}

int intToStr(int x, char str[])
{
  int i = 0;
  do
  {
    str[i] = (x % 10) + '0';//saving ASCII codes for digits of iPart 
    x = x / 10;
    i++;
  }
  while(x !=0);

  reverse(str, i);
  str[i] = '\0';
  return i;
}

void reverse(char *str, int len)
{
  int i = 0, j = len - 1, temp;
  while (i < j) 
  {
    temp = str[i];
    str[i] = str[j];
    str[j] = temp;
    i++;
    j--;
  }
}

```

**Output:**

```cpp
6.3

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [July 17, 2022, 12:02pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/18 "2022-07-17T12:02:44Z")

</div>

> [@GolamMostafa](#):
>
> ` Serial.println(n, 1);`

i don't believe there's an issue printing an arbitrary # of decimal digits using Serial.println (). the issue is getting around the loat limitation using sprintf()

```arduino
123.5
123.46
123.456
123.456

```

```arduino
// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    float x = 123.456;

    Serial.println (x, 1);
    Serial.println (x, 2);
    Serial.println (x, 3);
    Serial.println (x, 3);
}

// -----------------------------------------------------------------------------
void
loop ()
{
}

```

note that it is 0.46, not 0.45

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 17, 2022, 12:04pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/19 "2022-07-17T12:04:32Z")

</div>

> [@ALAA72](#):
>
> I dont want the foating number to round up...

Why?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 12:05pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/20 "2022-07-17T12:05:45Z")

</div>

> [@gcjr](#):
>
> i don't believe there's an issue printing an arbitrary # of decimal digits using Serial.println (). the issue is getting around the loat limitation using sprintf()

I have just implemented the algorithm of Post #4 @J-M-L.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 17, 2022, 12:06pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/21 "2022-07-17T12:06:48Z")

</div>

> [@anon57585045](#):
>
> Why?

It his liberty but ours duty as volunteers?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 17, 2022, 12:08pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/22 "2022-07-17T12:08:54Z")

</div>

We help people. If they ask for help with X, and they have an XY problem, giving them X is not helping.

If they can explain the need for X, then we're helping.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 17, 2022, 12:20pm UTC](https://forum.arduino.cc/t/sprintf-a-float-number/1013193/23 "2022-07-17T12:20:28Z")

</div>

it's always a good question to ask "why" (search for "_ask why seven times_" or "_The 7 levels of why_" (some variation with "_the Five Why's_.") for literature on the topic) - let's you challenge what you really have in mind.

[Next page](https://forum.arduino.cc/t/sprintf-a-float-number/1013193.md?page=2)
