# The syntax for converting float value into the scientific notation in Arduino ?

**URL:** <https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290>\
**Category:** Programming\
**Created:** [March 10, 2018, 8:16pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290 "2018-03-10T20:16:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![purvil\_477](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@purvil\_477](https://forum.arduino.cc/u/purvil_477)\
**Post date:** [March 10, 2018, 8:16pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/1 "2018-03-10T20:16:30Z")

</div>

I want to convert a float value to the scientific value, what's the syntax in Arduino IDE? for example,  
float f = 0.0032 should be printed like 3.2x10^(-3).

```cpp
Serial.println(float2s(f, 7));

```

this syntax is not working in Arduino IDE.

Please help me to fix it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 10, 2018, 8:48pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/2 "2018-03-10T20:48:58Z")

</div>

Do you have code for the function float2s()? If so, post it.

In any case, explain what you mean by "not working".

In C/C++ the function sprintf() can produce float to character strings in scientific notation, but that option is disabled by default in Arduino.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [March 10, 2018, 9:58pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/3 "2018-03-10T21:58:02Z")

</div>

I've written a sci function - [Arduino/libraries/MathHelpers at master · RobTillaart/Arduino · GitHub](https://github.com/RobTillaart/Arduino/tree/master/libraries/MathHelpers)

syntax quite straightforward

f=0.0032;

Serial.println(sci(f, 4));

prints 3.2000E-3

just like a calculator

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [March 10, 2018, 10:34pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/4 "2018-03-10T22:34:49Z")

</div>

Have a look at [dtostre()](http://www.nongnu.org/avr-libc/user-manual/group __avr__ stdlib.html#ga6c140bdd3b9bd740a1490137317caa44).

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 10, 2018, 10:51pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/5 "2018-03-10T22:51:47Z")

</div>

Servus Whandall. Bedeutet Dir der Buchtitel "The Burning City" von Niven und Pournelle etwas?

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [March 10, 2018, 10:52pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/6 "2018-03-10T22:52:50Z")

</div>

Ja. Habe ich vor langer Zeit mal gelesen. 😉

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 10, 2018, 10:58pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/7 "2018-03-10T22:58:23Z")

</div>

Klasse!

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [March 10, 2018, 11:02pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/8 "2018-03-10T23:02:47Z")

</div>

Ich bin ein großer Fan von Niven, Pournelle, Adams, Pratchett, ...

Puppenspieler, Crashlander, Rincewind oder Zweiblum wären auch gute Nicks gewesen. 😉

---

<div class="post-metadata">

**Author:** ![purvil\_477](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@purvil\_477](https://forum.arduino.cc/u/purvil_477)\
**Post date:** [March 11, 2018, 5:38am UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/9 "2018-03-11T05:38:01Z")

</div>

```cpp
String svstring;
                          svstring = String(svstring + txt_buf);

                          sv = svstring.toFloat();
                          float pv =2.0;
                          float sv;
                          int analogChannel = 0;
                          float sensorReading = analogRead(analogChannel);
                          pv = 2*((sensorReading*5)/1023);
                          pv = pow(10,1.5*(pv) - 12);
                          
                          if(sv>=pv)
                          {
                            digitalWrite(4, HIGH);
                            Serial.println("Writing HIGH");
                            client.println("HIGH");
                            //return true;
                          }
                          else if(sv<pv)
                          {
                            digitalWrite(4, LOW);
                            Serial.println("Writing LOW");
                            client.println("LOW");
                            //return false;
                          }

                          client.print("Analog input ");
                          client.print(analogChannel);
                          client.print(" is ");
                          client.print(pv,7);
                          //client.println(sci(pv,4));
                          client.print("\r\n");
                          
                        }
                    }

```

This is the middle part of my Arduino code...I want to convert float pv value into the scientific notation.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [March 11, 2018, 8:26am UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/10 "2018-03-11T08:26:43Z")

</div>

> purvil\_477:  
> ...  
> This is the middle part of my Arduino code...I want to convert float pv value into the scientific notation.

You need to install the MathHelpers library and add **#include "MathHelpers.h"** at the top of your code

- [Arduino/libraries/MathHelpers at master · RobTillaart/Arduino · GitHub](https://github.com/RobTillaart/Arduino/tree/master/libraries/MathHelpers)

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 11, 2018, 3:06pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/11 "2018-03-11T15:06:17Z")

</div>

> String svstring;

Not a good idea on Arduino. Strings cause memory problems and program crashes.

---

<div class="post-metadata">

**Author:** ![purvil\_477](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@purvil\_477](https://forum.arduino.cc/u/purvil_477)\
**Post date:** [March 13, 2018, 5:25pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/12 "2018-03-13T17:25:12Z")

</div>

Thanks a lot...you guys are awesome...

Thanks once again!🙂

---

<div class="post-metadata">

**Author:** ![deey](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@deey](https://forum.arduino.cc/u/deey)\
**Post date:** [June 27, 2019, 3:04pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/13 "2019-06-27T15:04:06Z")

</div>

> robtillaart:  
> I've written a sci function - [Arduino/libraries/MathHelpers at master · RobTillaart/Arduino · GitHub](https://github.com/RobTillaart/Arduino/tree/master/libraries/MathHelpers)

Great stuff. Thanks!

Is this fully tested?

---

<div class="post-metadata">

**Author:** ![AE35](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@AE35](https://forum.arduino.cc/u/AE35)\
**Post date:** [September 6, 2019, 6:54pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/14 "2019-09-06T18:54:23Z")

</div>

Hi guys - and robtillaart

I have this error when using the library (see attached picture)

Any thoughts why it would have such an error?

Thanks!

 ![mathhelpers_error.png](https://europe1.discourse-cdn.com/arduino/original/4X/4/7/8/478658b3ca7b4a2f8e2b532897ffea2c56a1b6c7.png)

---

<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:** [May 5, 2021, 11:03pm UTC](https://forum.arduino.cc/t/the-syntax-for-converting-float-value-into-the-scientific-notation-in-arduino/513290/15 "2021-05-05T23:03:47Z")

</div>

Closed on May 5, 2021, 11:03pm UTC


