# Calculation displays scientific format instead of a whole number

Joe Laviguer
Ranch Hand
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
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?

Joe Laviguer
Ranch Hand
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

Double.parseDouble something something

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

Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Did you look at the number formatting tutorial?

Joe Laviguer
Ranch Hand
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
Posts: 1809
12
It might!

By the way, welcome to JavaRanch

Joe Laviguer
Ranch Hand
Posts: 30
Janeice DelVecchio wrote:

By the way, welcome to JavaRanch

Thank you!

Joe Laviguer
Ranch Hand
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
Posts: 20665
65

Joe Laviguer
Ranch Hand
Posts: 30
Thanks Rob; I edited my post.

W. Joe Smith
Ranch Hand
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
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
Marshal
Posts: 21497
84
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

Joe Laviguer
Ranch Hand
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
Posts: 1809
12
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
Posts: 50196
79
Why did you say "hex"? That isn't hex, that's decimal in scientific notation.

Joe Laviguer
Ranch Hand
Posts: 30
Campbell Ritchie wrote:Why did you say "hex"? That isn't hex, that's decimal in scientific notation.

You are correct...I forgot to edit that part of my post

Joe Laviguer
Ranch Hand
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
Posts: 50196
79
No problem; I can change the title for you.

Did you find my link any use?

Campbell Ritchie
Sheriff
Posts: 50196
79
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
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
Posts: 1809
12
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
Posts: 50196
79
Quicker to use the %n tag after printf.

Campbell Ritchie
Sheriff
Posts: 50196
79
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
Posts: 30
Okay I think we're good now lol

Thank you! I really hope I'm learning something lol