LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2003, 09:28 PM   #16
darin3200
LQ Guru
 
Registered: Dec 2002
Distribution: Gentoo!
Posts: 1,153

Original Poster
Rep: Reputation: 45

Currently I am try to write a simple program and I am getting some trouble from the complier. Here is what I have
Code:
class moon {
	public static void main(String args[]) {
int normal;
int moon;
int counter;
counter = 0;
for(normal = 0; normal <= 250; normal = normal+5) {
moon = normal * .17;
System.out.println( + normal + " pounds on earth would be " + moon + " pounds on the moon " );
counter++;
if(counter == 10) {
System.out.println();
counter = 0;
}
}
}
}
now the program will work great when I replace int normal and int moon with double normal and double moon but the problem is i don't want the really long decimals, just rounded numbers. When I try to complie with int instead of double I get this from the complier
Code:
moon.java:8: possible loss of precision
found   : double
required: int
moon = normal * .17;
              ^
1 error
Is there anyway around this?
 
Old 07-01-2003, 10:16 PM   #17
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by darin3200
Code:
moon = normal * .17;
I'm not too good with Java, but try:
moon = intValue( normal * .17);

Cheers,
Tink
 
Old 07-01-2003, 11:00 PM   #18
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Take a look at the Integer and Double classes. I don't have my references with me right now (I usually leave them at work). But the Double class has all kinds of formatting capaiblities.

Such as if you have a double value called dblVal then you could use code such as this...

double dblVal;
int intVal;

*** code to set value of dblVal ***

Double dblClassInstance = new Double (dblVal);
intVal = dblClassInstance.intValue ();

however this WILL cause loss of precision. such that if dblVal = 3.12159 then the resulting intVal may be just 3.

perhaps a better option would be to print a rounded value of 3.12?
or perform some type of formatting in the print statement... while I'm not certain I think I remember reading somewhere that Java println functions can handle to some extent formatting like what is available in C's printf statements.

Check out the class definition / descriptions here:
http://java.sun.com/j2se/1.3/docs/ap...ng/Double.html

here's a link about formatting strings in Java:
http://developer.java.sun.com/develo...mming/sprintf/

hth,
jpbarto
 
Old 09-11-2003, 07:54 PM   #19
AlaskaLinux
LQ Newbie
 
Registered: Sep 2003
Posts: 1

Rep: Reputation: 0
Java can't put a decimal value into an int ever, so you have to set your normal and moon for double and then at the end convert them to int
 
Old 09-11-2003, 08:34 PM   #20
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
As an example this will round my double to 212.24
I

Code:
import java.text.DecimalFormat;

public class dec
{
  public static void main(String args[])
  {


   double myDouble=212.235566;

   DecimalFormat myformat=new DecimalFormat("0000.00");

   String temp=myformat.format(myDouble);

   myDouble=Double.parseDouble(temp);

   System.out.println("My Double is now :" + myDouble);
   }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DISCUSSION: Beginning with Java darin3200 LinuxAnswers Discussion 28 03-27-2020 11:45 PM
Just the beginning.... woodworker97 Mandriva 5 05-20-2005 01:53 AM
A new beginning... LK873 Mandriva 13 03-08-2005 06:59 AM
very beginning vegpl Linux From Scratch 13 06-26-2004 11:56 AM
Beginning Fatal_3rr0r Programming 4 06-09-2003 08:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration