Go Down

Topic: how to ' convert data types to get only one decimal point ' ? (Read 2181 times) previous topic - next topic


May 01, 2013, 09:38 am Last Edit: May 01, 2013, 09:40 am by Dileesha Reason: 1
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 .


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()
 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);
 // Print a message to the LCD.
// lcd.print("Volt Amp Watt");
 lcd.setCursor(0, 0);
 lcd.print("Version 2.4  ");
 lcd.setCursor(0, 0);


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 .
Dileesha Amarasena


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).
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com


Does the Arduino version of sprintf() support floats directly ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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.
Don't PM me for help as I will ignore it.


@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 ?


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

Dileesha Amarasena


May 01, 2013, 10:04 am Last Edit: May 01, 2013, 10:09 am by marco_c Reason: 1
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
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com



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 Amarasena

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131