Further details:
It is just one of the rules of the java language that to get a double out of any expression at least one of the terms has to be a double or float. So 10/100 will not give you 0.1. It will give you 0. If you had 10.0/100, then you would get 0.1. In other words, if all the operands on an operator are int, or byte or short or char (i.e. no operand is a float or a double), an integral operation will be performed and an int will be returned.
Therefore, you should see it like this:
amount = 1 - rate/100*1 - rate/100;
amount = 1 - 10/100*1 - 10/100;
amount = 1 - 0*1 - 0;
amount = 1 - 0 - 0;
amount = 1;
Since the value of the right hand side expression is an int, you can define amount as int, long, or float as well besides double. Had the expression returned 1.0 i.e. a double, you would have had to define amount as double.
You may want to read more here: http://docs.oracle.com/javase/specs/jls ... ls-15.17.2
HTH,
Paul.
About Question enthuware.ocajp.i.v7.2.1183 :
Moderator: admin
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1183 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1183 :
This came into my mind so came to look for an answer. Your explanation here on double was very useful, Thank you.
-
- Posts: 5
- Joined: Tue Dec 08, 2015 9:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1183 :
>> Given the following LOCs:
What's a LOC please?
What's a LOC please?
-
- Site Admin
- Posts: 10075
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1183 :
Lines of code http://c2.com/cgi/wiki?LinesOfCode
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue Dec 08, 2015 9:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1183 :
Ah thanks, not come across that abbreviation before.
Who is online
Users browsing this forum: No registered users and 15 guests