• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculation displays scientific format instead of a whole number

 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the number formatting tutorial?
 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might!

By the way, welcome to JavaRanch
 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:

By the way, welcome to JavaRanch


Thank you!
 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob; I edited my post.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you say "hex"? That isn't hex, that's decimal in scientific notation.

Helpful Java™ Tutorials link.
 
Joe Laviguer
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem; I can change the title for you.

Did you find my link any use?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quicker to use the %n tag after printf.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I think we're good now lol



Thank you! I really hope I'm learning something lol
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic