how to ' convert data types to get only one decimal point ' ?

Hi friends,

I want to know how to convert an obtained double or float value into a value with only ONE decimal places . ( ex. 230.0 )

Currently what i get as the output is something like 230.00 . I don't need two decimal points, I want only one .

I'm a newbie to programming . So please be kind enough to modify my code, or give me an example .

Thanks.
Dileesha.

My code for arduino is as below

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 9, 8, 7, 6, 5);
void setup()
{
Serial.begin(9600);

emon1.voltage(2, 234.26, 1.7); // Voltage: input pin, calibration, phase_shift
emon1.current(3, 8.65); // Current: input pin, calibration.
lcd.begin(16, 2);
// pinMode(led,OUTPUT);
// digitalWrite(led, HIGH);
lcd.clear();
// Print a message to the LCD.
// lcd.print("Volt Amp Watt");
lcd.setCursor(0, 0);
lcd.print("Version 2.4 ");
delay(2000);

lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Amps");

}

void loop()
{
emon1.calcVI(200,1000); // Calculate all. No.of half wavelengths (crossings), time-out
emon1.serialprint(); // Print out all variables
lcd.setCursor(6, 1);
double Irms = emon1.calcIrms(1480); // Calculate Irms only
lcd.print(Irms); // Irms
}

PLEASE NOTE : I also attached the complete EmonLib library i used as in a ZIP file . That zip folder contains both .CPP file and .H file .

EmonLib.zip (5.68 KB)

Use sprintf() to format the string before you use it, into a character array and then print the character array. There is an option to use a specific number of decimal points as part of the %format string (%.1f from memory, but better look it up).

Does the Arduino version of sprintf() support floats directly ?

If the lcd.print uses the arduino built in print class then referring to this page you can add an extra parameter to the print to specify the number of decimal places to print to.

@macro c

Thanks for your comment . Btw I'm new to this programming so I really don't understand how to implement the change you suggested . Can you please give a simple try to edit my code ?

@UKheli

I really don't know friend, but I think @macro c can answer your question .

Riva probably has the easiest answer for you.

@ukhelibob - good question and I can't see an answer. Give other restrictions on floats, I expect probably not.

Update: here is a solution http://arduino.cc/forum/index.php/topic,146638.0.html

@Riva

You are a hero to me !

Yes it worked for me . Thanks so much @Riva .

If any newbie looking for a solution, as Riva said please follow this link .

Thanks for all who commented .
Dileesha.