It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Calculation displays scientific format instead of a whole number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculation displays scientific format instead of a whole number" Watch "Calculation displays scientific format instead of a whole number" New topic
Author

Calculation displays scientific format instead of a whole number

Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I'm playing with a java program to calculate how many minutes or seconds are in x number of years i.e. a person's age.

I use Double dAge for the input, then when I tell it to multiply whatever the input is times the number of minutes in a year (535600), or seconds (31536000) I get a display result of 1.yada-yada-yada instead of the actual result in a whole number.

So how can I tell it to display the result as a whole number instead of in scientific notation? When I display number of days, months, hours, the display result is in whole numbers as I would expect it to.

I also tried using input variables for the minutes and seconds as Long lminutes = 535600L and the same for seconds, but the display again shows in scientific notation.

Thanks!
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Are you using an IDE or command line?

Can you post your code so we can see if it could be something in there?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I'm using netbeans IDE, and unfortunately my code is at home (ssssshhhhhhh don't tell anyone I'm reading message boards at work). Just for clarification, the calculations come out fine, its the display I'm playing with. Thanks.

let's see if I can remember some of the code I wrote, limited:

Double dAge = 0.0; //input for age, in years

keyboard.readLine();

Double.parseDouble something something

System.out.println(dAge * 525600 + " minutes old.");
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Did you look at the number formatting tutorial?


When you do things right, people won't be sure you've done anything at all.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I wish I had some way to test this at work, but if I change this:

System.out.println(dAge * 525600 + " minutes old.");

TO

System.out.format("%d%n minutes old." (dAge * 525600));

will that give me what I'm looking for?

Thanks for the help.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

It might!

By the way, welcome to JavaRanch
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Janeice DelVecchio wrote:

By the way, welcome to JavaRanch


Thank you!
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Okay pulled some sample code from my class and edited it ( again, I'm using NetBeans IDE):



Sample resulting output:
LastName, FirstName
n years old. (displays properly)
n months old. (displays properly)
n days old. (displays properly)
n hours old. (displays properly)
n minutes old. (displays in scientific notation)
n seconds old. (displays in scientific notation)

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Thanks Rob; I edited my post.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Joe Laviguer wrote:Thanks Rob; I edited my post.


Check your {} matching sets. I think you have an extra } in there. Not exactly the problem you were asking about, but an issue nonetheless.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Still not having any luck displaying the larger numbers as anything but scientific notation:

results are:

2.41776E7 minutes old.
1.450656E9 seconds old.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

Joe Laviguer wrote:Still not having any luck displaying the larger numbers as anything but scientific notation:

results are:

2.41776E7 minutes old.
1.450656E9 seconds old.


Did you try any of the formatting stuff, that you were given a reference to?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
I did, but I guess I'm not understanding it well enough (I'm just starting out in this programming stuff) to make it work.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Joe Laviguer wrote:I did, but I guess I'm not understanding it well enough (I'm just starting out in this programming stuff) to make it work.


You should find a way to format it to stop at the the tenths place....

I only say this because I think it will work and know it can be done.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38858
    
  23
Why did you say "hex"? That isn't hex, that's decimal in scientific notation.

Helpful Java™ Tutorials link.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Campbell Ritchie wrote:Why did you say "hex"? That isn't hex, that's decimal in scientific notation.

Helpful Java™ Tutorials link.


You are correct...I forgot to edit that part of my post
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Okay I got it working now. I brought in my netbook and typed in the code manually line by line. I could have sworn I tried this on my home PC so I'll have to go look at my code at home to see what's wrong with it.

I changed:



I changed that to:



Thanks to everyone for their help!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38858
    
  23
No problem; I can change the title for you.

Did you find my link any use?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38858
    
  23
You might do well to consider using numbers with the %f tag. Try %.0f. Look through the Formatter class and see how %f compares with %e and %g. You can also see what %6.2f %6.0f and %.2f mean, but you will have to read carefully; it's not easy to understand.
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Okay I plugged in %.0f and that eliminated the 6 trailing zeros, but now when I'm using printf instead of println, there's no break at the end of the individual lines, it all runs on in one line i.e.

Lname, FName
xx years old.xx months old.xx days old. etc.

do I need a "break;" or something in there?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Golly, I need a break -- coffee, thank you.

What you can do is put a System.out.println() with no parameters and skip a line, or (my preferencem but I don't know how it will work with printf) use a newline \n to create a new line inside your printf.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38858
    
  23
Quicker to use the %n tag after printf.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38858
    
  23
Better to use %n than \n (as it says in the JavaTutorials link I quoted earlier) because the result of %n is corrected for the operating system. You won't notice any difference for 99% of the time . . .
Joe Laviguer
Ranch Hand

Joined: Dec 08, 2009
Posts: 30
Okay I think we're good now lol



Thank you! I really hope I'm learning something lol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculation displays scientific format instead of a whole number