Go Down

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

Dileesha

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 .

Thanks.
Dileesha.


My code for arduino is as below

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

marco_c

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

UKHeliBob

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.

Riva

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.
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

Dileesha

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

Dileesha Amarasena

marco_c

#5
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

Dileesha

@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 .
http://arduino.cc/en/Serial/Print

Thanks for all who commented .
Dileesha.
Dileesha Amarasena

Go Up