• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting floating point number to int  RSS feed

 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
anyone please could you give me the output of this:

int x = (int) 24.6;


thanks
raj
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output did you get when you tried it yourself?
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am studying head first java. it is giving an example. so according to me the output was supposed to be 25. is it correct?
thanks
raj
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it out for yourself and you will have the definitive answer. Let us know what output you actually get, not what you think you'll get, what you actually get.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cannot try at the moment because it is just giving this single line example. so would like to know.
i will try myself when i come accross in coming chapters.
if you just give me answer i will have rough idea when i do related in coming chapters.
thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, as a beginner, it is a good idea to get into the habit of compiling and testing code.

Raj Gurung wrote:i cannot try at the moment because it is just giving this single line example. so would like to know.


Why not? You don't have a "hello world" example that you can insert and test your one line with? All books generally start with that.

Henry
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry.
i will try right now.
got some idea i think.
thanks for your support.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our Hello World article will give you an example of how to get started with a fully executable Java program. With some small modifications, you can try out your example. See how you get on.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks i will try it henry.
thanks a lot.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry,
i ran program and got output as 24.
i was thinking output to be 25, as 24.6 is greater than 24.5 so output will be 25.
But it took lower that is 24.
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what conclusions do you draw from that?

P.S. Please UseAMeaningfulSubjectLine -- I have adjusted the subject as an example
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
conclusion is it discard the fractions in output.
is it correct??

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Gurung wrote:conclusion is it discard the fractions in output.
is it correct??

I'd suggest running some additional test cases...try something like 24.999 or even something like -24.1.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, instead of infinit testing cases (I agree practising is a must though), you can just read about conversions http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have changed -http... to http... in your last post and you see the link now appears as a proper hyperlink.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I was posting from a tablet using the mobile version of the site, so I just pasted the URL.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj, also test decimal numbers for float, double, int, long types.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!