Pages: [1]   Go Down
Author Topic: how to ' convert data types to get only one decimal point ' ?  (Read 644 times)
0 Members and 1 Guest are viewing this topic.
Colombo
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Bachelor in EEE Student
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .

* EmonLib.zip (5.68 KB - downloaded 3 times.)
« Last Edit: May 01, 2013, 02:40:56 am by Dileesha » Logged

Dileesha Amarasena

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1184
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3482
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Colombo
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Bachelor in EEE Student
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Dileesha Amarasena

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1184
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: May 01, 2013, 03:09:48 am by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Colombo
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Bachelor in EEE Student
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dileesha Amarasena

Pages: [1]   Go Up
Jump to: