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

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).

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

this syntax is not working in Arduino IDE.

Please help me to fix it.

Thanks in advance.

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.

I've written a sci function - Arduino/libraries/MathHelpers at master · RobTillaart/Arduino · GitHub

syntax quite straightforward

f=0.0032;

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

prints 3.2000E-3

just like a calculator

Have a look at dtostre().

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

Ja. Habe ich vor langer Zeit mal gelesen. :wink:

Klasse!

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

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

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.

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

String svstring;

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

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

Thanks once again!:slight_smile:

robtillaart:
I've written a sci function - Arduino/libraries/MathHelpers at master · RobTillaart/Arduino · GitHub

Great stuff. Thanks!

Is this fully tested?

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!