0

Isnt 15/4 = 3.75? But why is this answer 3

3rd Feb 2019, 12:28 AM
Sum Ting Wong
Sum Ting Wong - avatar
3 Answers
+ 5
There is a datatype int (integer) and a datatype float (number with decimals). If you divide an integer by an integer, the result will again be an integer. So basically, the .75 gets dropped.
3rd Feb 2019, 12:37 AM
HonFu
HonFu - avatar
0
public class Program { public static void main(String...hn) { System.out.println(15/4); // by default if you divided integer with integer it will remove digit after comma System.out.println(15/4.0); // if you supposed to get decimal number, simply just add comma followed by number what ever you want System.out.println((double)15/4); // this is another way to get decimal number as result' it called type casting' // cmmiw, cheers ' } }
3rd Feb 2019, 12:52 AM
Nopal Opal
Nopal Opal - avatar
0
which language? Python makes this true 15/4 is converted to a float. Other languages depends on specific implementation. C, Pascal maintain type (int/int=int) javascript, doesn't truncates, so you'll get your 3.75
3rd Feb 2019, 3:14 AM
Mauricio Martins
Mauricio Martins - avatar