• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert double to int?  RSS feed

 
Cameron O'Neill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a program that will display your target heart rate. I am having a problem getting the target heart rate to be a int number and not a double. I know I have to convert it somehow, but I am not entirely sure how. Its most likely very simple, I would just like to figure this out, thanks.

here is the code:



As it is, Target heart rate will print out a number with a lot of decimals. I don't want the number to have any decimals.

Thanks in advance.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cam O'Neill wrote:I am trying to write a program that will display your target heart rate. I am having a problem getting the target heart rate to be a int number and not a double. I know I have to convert it somehow, but I am not entirely sure how.

Have a look at Math.round().

Winston
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to "cast" a double to an int.

Say you have:



You cast to an int like this:



Now i = 12. Hope that helps.
 
Cameron O'Neill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which statement would I cast an int or use the Math.round?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch and thanks for going back and adding the code tags.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cam O'Neill wrote:In which statement would I cast an int or use the Math.round?

At the point at which you want to use integer rather than floating point numbers.
 
Cameron O'Neill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch and thanks for going back and adding the code tags.


Thanks, I cant seem to figure this out. I tried using the Math.round() and still does not work.



Am I going in the right direction? I also changed the doubles for the age and rHR variables to int, but that still does not work.

This is probably very simple, I just cant seem to figure it out.

Also tried to cast the double to int:


Still does not work.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using the Math.round() and still does not work.

The Math.round() method returns an int, it doesn't change the type (or value) of the parameter to an int.

I also changed the doubles for the age and rHR variables to int, but that does still not work.

It's sensible to store the input value as an int if the calculation requires an int value but if you call getNextInt() and the user has entered their age as 39.75 what will happen?
 
Cameron O'Neill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured it out.



I had it at %f.0 before, forgot the point and number had to come before the f.

Thanks for the help!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't keep editing old posts to structurally change the contents especially if someone has already replied as it makes it hard for other people to understand the thread. Fixing mistakes, typos, formatting etc is ok.

Doing hr = (int) hr; does not convert the double to an int, it just removes the floating point part of the value. It convert a double to an int you must store the result of the cast or of calling Math.round() in a variable of type int.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cam O'Neill wrote:Figured it out...

No you haven't. You've found something that compiles, which is NOT the same.

If you're dealing with heart rates, you're possibly (??) OK; but if you're dealing with somebody's money, you'd better make sure you're right before you say so.

Understand what you've done with
hR = (int) hR;
before you say 'Eureka'.

Winston
 
Cameron O'Neill
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Please don't keep editing old posts to structurally change the contents especially if someone has already replied as it makes it hard for other people to understand the thread. Fixing mistakes, typos, formatting etc is ok.

Doing hr = (int) hr; does not convert the double to an int, it just removes the floating point part of the value. It convert a double to an int you must store the result of the cast or of calling Math.round() in a variable of type int.


Sorry about that, I will make sure to double check before posting or just post a separate post next time. I removed the hr = (int) hr; from the code. The %.0f worked for me. I guess the title should really be: how to remove decimal points after a number lol.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!