# Output a Two Decimal Value

• 07-17-2011, 12:26 PM
Hillsid3
Output a Two Decimal Value
It's suppose to display 12.50 but instead it's displaying 0.0 :(whew):

Code:

```import java.io.*; public class InputOutput {         public static void main(String[] args) {                 String base="";                 String height="";                                 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));                 try {                         System.out.print("Input base value = ");                         base = input.readLine();                         System.out.print("Input height value = ");                         height = input.readLine();                 } catch(IOException e) {                         System.out.print("Error");                 }                                 float area = 1/2*(Integer.parseInt(base)* Integer.parseInt(height)); System.out.println("The Area of the right triangle is "+ area);         } }```
• 07-17-2011, 12:41 PM
JosAH
Integer division (as in 1/2) chops of the entire fractional part of the quotient, so 1/2 == 0. Either multiply by 0.5 or do the division using floating point numbers, as in 1.0/2.0

kind regards,

Jos
• 07-17-2011, 12:46 PM
Hillsid3
But i need to use the formula 1/2*bh for computing for the area of the triangle. What should I change in the code? :=(:
• 07-17-2011, 12:48 PM
JosAH
Quote:

Originally Posted by Hillsid3
But i need to use the formula 1/2*bh for computing for the area of the triangle. What should I change in the code? :=(:

0.5*bh?

kind regards,

Jos (<--- rocket scientist)
• 07-17-2011, 12:58 PM
Hillsid3
no, i mean i still have to use 1/2 not 0.5
• 07-17-2011, 01:09 PM
JosAH
Quote:

Originally Posted by Hillsid3
no, i mean i still have to use 1/2 not 0.5

Who said so? b.t.w. 1/2 equals 0 in Java, so you can't use it here, you have to rewrite your expression a bit ...

kind regards,

Jos
• 07-17-2011, 01:21 PM
Fubarable