Help, how to extract the integer part

Solved
Thinathenet Posted messages 23 Status Membre -  
Thinathenet Posted messages 23 Status Membre -
Hello,
How can I extract the integer part of a real number using Java, and thank youuu :) !!!

2 réponses

walidovich_85 Posted messages 698 Status Membre 73
 
Hello,

The primitive that represents real numbers is "double".
So, to extract the integer "int" from this real number, just do as in this example:

 double d = -12.91; System.out.println("d= " + d); int i = (int) d; System.out.println("i= " + i); 


The result:
 d= -12.91 i= -12 


Good luck

Being normal is boring... being geek is interesting
1
walidovich_85 Posted messages 698 Status Membre 73
 
If I remember correctly from my math classes, I think that for the example above, the integer part of -12.91 is -13. So, we need to differentiate between positive and negative real numbers.
0
Thinathenet Posted messages 23 Status Membre 1
 
What you did, Walidovich, is called rounding and not the integer part!!!
So the part is (-12) and the integer part with rounding is (-13).
0
Thinathenet Posted messages 23 Status Membre 1
 
And by the way, what you suggested to me worked really well, it's just what I wanted, thank you so much!!!
0
walidovich_85 Posted messages 698 Status Membre 73
 
Consult KX's suggestions; they are more rigorous and methodical.
And for a former specialist in Mathematics, it was serious and cool to forget the fundamentals.
0
Thinathenet Posted messages 23 Status Membre 1
 
Alright if you say so!
Since it's the first time I'm programming with Java and on top of that for my final thesis, it's a bit difficult for me!
0