# How do I add decimal places and not round 29 i want 29.92

how do I increase the decimal place in this code string computation, when i add it at end it just uploads the 2 to my thingspeak

such as number4 right now its posting 29 i'd like 29.xx 2 more places when i try this number4 = (P*0.0295333727,2); it just prints the 2

`````` number1 = (dht.toFahrenheit(temperature));
number2 = (humidity);
number3 = (lux);
number4 = (p0*0.0295333727);
number5 = (a*3.28084);
//number6 = (50);
//number7 = (50);
//number8 = (50);
``````

thank you in advance i know it must be simple I've only been learning code for 2.5 weeks and Ive managed to build a weather station that uploads to thingspeak from an ESP32 im pretty proud of that but the code gives me nightmares one little } out of place causes me hrs of headache but im new so I'll learn

Post ALL the code. Snippets are useless; we need to know how you declared the variables.

The extra parentheses can be dropped.

i know it must be simple I've only been learning code for 2.5 weeks

It is if you start at the beginning, and learn the basics first.

its working in serial monitor i get the 2 decimal places but I don’t get it with this upload to thingspeak.
I was hoping you could just identify my coding error in line

number4 = (P*0.0295333727,2); <— I just thought this was typed wrong

i’ll post my embarrassing code in a bit.

`number4 = (P*0.0295333727,2); `.What is the ",2" doing there?

i thought that's how you make it 2 decimal places aka 29.92 instead of 29

Edwin101: i thought that's how you make it 2 decimal places aka 29.92 instead of 29

It doesn't.

Edwin101: i thought that's how you make it 2 decimal places aka 29.92 instead of 29

It does in a Serial.print(). But not in a calculation/assignment.

If we could see how all your variables are defined and how you try to "upload to thingspeak" it would be a lot easier to help.

Steve

here is my ugly code please dont judge im 2.5 weeks old

``````#include "DHTesp.h"
#include <BH1750FVI.h>
#define DHTpin 19    //original said D15 of ESP32 DevKit
#include <SFE_BMP180.h>
#include <Wire.h>
#define ALTITUDE 421.0 // Altitude of Farm in PA in meters

#include "ThingSpeak.h"
#include "secrets.h"
#include <WiFi.h>

char ssid[] = SECRET_SSID;   // your network SSID (name)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = (0);
int number2 = (0);
int number3 = (0);
int number4 = (0);
int number5 = (0);
//int number6 = (0);
//int number7 = (0);
//int number8 = (0);
String myStatus = "";

// You will need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 pressure;

DHTesp dht;
// Create the Lightsensor instance
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);

void setup() {
Serial.begin(115200);  //Initialize serial

WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);  // Initialize ThingSpeak
Serial.begin(115200);

//bmp180start
// Initialize the sensor (it is important to get calibration values stored on the device).

if (pressure.begin())
Serial.println("BMP180 init success");
else
{
// Oops, something went wrong, this is usually a connection problem,
// see the comments at the top of this sketch for the proper connections.

Serial.println("BMP180 inielset fail\n\n");
while(1); // Pause forever.
}
//bmp180end

LightSensor.begin();
dht.setup(DHTpin, DHTesp::DHT11); //for DHT11 Connect DHT sensor to GPIO 19
//dht.setup(DHTpin, DHTesp::DHT22); //for DHT22 Connect DHT sensor to GPIO 19
}

void loop() {

char status;     //bmp180 stuff
double T,P,p0,a;  //bmp180 stuff

//bmp180
// Loop here getting pressure readings every 10 seconds.

// If you want sea-level-compensated pressure, as used in weather reports,
// you will need to know the altitude at which your measurements are taken.
// We're using a constant called ALTITUDE in this sketch:

Serial.println();
Serial.print("provided altitude: ");
//Serial.print(ALTITUDE,0);
//Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");

// If you want to measure altitude, and not pressure, you will instead need
// to provide a known baseline pressure. This is shown at the end of the sketch.

// You must first get a temperature measurement to perform a pressure reading.

// Start a temperature measurement:
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.

status = pressure.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);

// Retrieve the completed temperature measurement:
// Note that the measurement is stored in the variable T.
// Function returns 1 if successful, 0 if failure.

status = pressure.getTemperature(T);
if (status != 0)
{
// Print out the measurement:
Serial.print("temperature: ");
//Serial.print(T,2);
//Serial.print(" deg C, ");
Serial.print((9.0/5.0)*T+32.0,2);
Serial.println(" deg F");

// Start a pressure measurement:
// The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.

status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);

// Retrieve the completed pressure measurement:
// Note that the measurement is stored in the variable P.
// Note also that the function requires the previous temperature measurement (T).
// (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// Function returns 1 if successful, 0 if failure.

status = pressure.getPressure(P,T);
if (status != 0)
{
// Print out the measurement:
Serial.print("absolute pressure: ");
//Serial.print(P,2);
//Serial.print(" mb, ");
Serial.print(P*0.0295333727,2);
Serial.println(" inHg");

// The pressure sensor returns abolute pressure, which varies with altitude.
// To remove the effects of altitude, use the sealevel function and your current altitude.
// This number is commonly used in weather reports.
// Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
// Result: p0 = sea-level compensated pressure in mb

p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
Serial.print("relative (sea-level) pressure: ");
//Serial.print(p0,2);
//Serial.print(" mb, ");
Serial.print(p0*0.0295333727,2);
Serial.println(" inHg");

// On the other hand, if you want to determine your altitude from the pressure reading,
// use the altitude function along with a baseline pressure (sea-level or other).
// Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
// Result: a = altitude in m.

a = pressure.altitude(P,p0);
Serial.print("computed altitude: ");
//Serial.print(a,0);
//Serial.print(" meters, ");
Serial.print(a*3.28084,0);
Serial.println(" feet");
}
else Serial.println("error retrieving pressure measurement\n");
}
else Serial.println("error starting pressure measurement\n");
}
else Serial.println("error retrieving temperature measurement\n");
}
else Serial.println("error starting temperature measurement\n");

//endbmp180

//DHT-11 start
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.println("Status\tHumidity (%)\tTemperature (F)\tHeatIndex (F)");
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
///Serial.print(temperature, 1);
///Serial.print("\t\t");
Serial.print(dht.toFahrenheit(temperature), 1);
Serial.print("\t\t");
///Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1);
///Serial.print("\t\t");
Serial.println(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1);
//DHT-11 end

//GY-30 sensor start
uint16_t lux = LightSensor.GetLightIntensity();
Serial.print("Light: ");
Serial.println(lux);
//GY-30 sensor end

// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}

// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
ThingSpeak.setField(3, number3);
ThingSpeak.setField(4, number4);
ThingSpeak.setField(5, number5);
//ThingSpeak.setField(6, number6);
//ThingSpeak.setField(7, number7);
//ThingSpeak.setField(8, number8);

// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 < number2){
myStatus = String("field1 is less than field2");
}
else{
myStatus = String("field1 equals field2");
}

// set the status
ThingSpeak.setStatus(myStatus);

// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}

// change the values
//number1++;
number1 = (dht.toFahrenheit(temperature));
number2 = (humidity);
number3 = (lux);
number4 = (p0*0.0295333727);
number5 = (a*3.28084);
//number6 = (50);
//number7 = (50);
//number8 = (50);

delay(20000); // Wait 20 seconds to update the channel again
``````

`int number4 = (0);` Is this the number4 variable you are trying to store a fractional value in?

See the problem?

ah so int number are whole numbers right? so id want a .......

runs to google to find out

Oh and THANKYOU! I appreciate you pointing me on the scent i’m @ Adruino home page learning functions, I’ll eventually find the right one. I hate being new.

YES YES YES, I found it read it and learned it

my change worked

``````//int number4 = (0);
String number4 = String(0, 3);
``````

OK so String (capital S) may seem handy now, but in the long run it will probably cause you headaches and cause the Arduino to crash. The reason is because the Arduino has relatively little memory compared to a PC and the String object can cause this small amount of memory to become fragmented and run out. Have a read over this https://www.arduino.cc/reference/en/language/variables/data-types/string/ page to get an idea of the more accepted alternative to String (capital S).

ugh, even if I have it resetting to zero everytime it runs the code? `String number1 = String(0, 1);`

I'm seriously kindergarten level here, I learn as stuff doesn't work, what do you suggest.

Don't use Strings. Period.

Use floating point variables to store numbers with decimal fractions.

``````float x=3.14159;
Serial.println(x);
Serial.println(x,2);
Serial.println(x,4); //to see the difference
``````

I got your float to work and while I can adjust the serial.println to show shorter numbers its uploading a 5 digit decimal to my thingspeak page gives me decimal place like this 28.52612

I tried adding the (0, 2) on end but it doesn't work

``````///String number4 = String(0, 2);
float number4 = (0);

number4 = (P*0.0295333727);
``````

I tried adding the (0, 2) on end but it doesn’t work

Posting snippets doesn’t work either.

(All) (those) (extra) (parentheses) (in) (your) (code) (just) (make) (you) (look) (clueless). Get rid of them, so it is harder for us to see that you are clueless.

If you are sending a float to thingsqueak, and it shows the float to 5 decimal places, the problem is NOT in the Arduino code.

If you are converting the float to a string, and sending the string with 5 decimal places, then the problem is in your code.

I am (clueless) I don't know code I'm trying to learn from scratch. when I take out the () i get more errors so I leave them in and it works my whole code is posted in this thread, a few replies back. i just posted the one line snipit change i made.

i do thank you for looking, and I do apologize for being new and not understanding yet.

my real concern is that I may be floating super long numbers and uploading unnecessary data every 20 seconds.

when I take out the () i get more errors

It is these ( and ) in particular that are unnecessary

``````int number1 = (0);
int number2 = (0);
int number3 = (0);
int number4 = (0);
int number5 = (0);
``````

I'm trying to learn from scratch.