1

If I want to calculate 76/266 = 28.57% in java, what's the best data type to use?

So far, I have:

int x = 76;
int y = 266;
float z = (x * 100) / y;

But doing this, I get 28.0 as an answer. I need to get an answer rounded to the nearest hundredth place. Thanks.

user2360087
  • 17
  • 1
  • 1
  • 6

4 Answers4

6

In Java and some other programming languages, there is something called integer arithmetic, which says that if you do (in your case):

int / int = int

In your code, you are doing

(int * int) / int   <=>   int / int = int

Solutions:

Method 1: Something you can do to get a float is to use a float operand. In your case it can be the 100:

float z = (x * 100.0f) / y;

Here, the operation is

(int * float) / int   <=>   float / int = float

Method 2: Another way to solve this is to cast an integer to a float:

float z = (x * 100) / (float)y;   // int * int / float = float
float z = (float)x * 100 / y;   // float * int / int = float

Method 3: As @webSpider mentioned in his answer, you can just declare the variables x and y as float to avoid these problems.

Edit: To round your float result, you can try this:

float z = Math.round(result * 100) / 100f;

where the number of zeros of 100 is the number of decimal places. Note that 100f will be a float because of the postfix f.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
3
float x = 76;
float y = 266;
float z = x * 100 / y;
//=28.571428

If you want to round it, use:

double x = 76;
double y = 266;
double z = Math.round(x * 100 / y* 100.0) / 100.0;
//=28.57

btw, as you see you don't need the parenthesis in your calculation there is a operator precedence ...

griffon vulture
  • 6,594
  • 6
  • 36
  • 57
2

Maybe you can use java.math.BigDecimal for your calculation. I believe this is the highest precision datatype in Java

BigDecimal d1 = new BigDecimal(67.67);
BigDecimal d2 = new BigDecimal(67.68);
BidDecimal d3 = d1.divide(d2); // d1 + d2 is invalid
indivisible
  • 4,892
  • 4
  • 31
  • 50
paradox
  • 377
  • 3
  • 12
  • using bigdecimal will also help you avoid pitfalls associated with floating pointing arithmetic. – Scary Wombat Jun 18 '14 at 06:46
  • 1
    Might want to mention that you'll want to be careful for `ArithmeticExceptions` if the result is an infinitely repeating decimal, unless you have limited precision. – awksp Jun 18 '14 at 06:50
  • When using BigDecimal you should use the constructor that takes String as parameter. If you use the constructor which takes double the BigDecimal you get back is the exact representation as the double you passed in! – laitinen Jun 18 '14 at 06:57
  • @user2360087, A BigDecimal "can" take a String but it "may" take any number of arguments. Please see [the Java docs](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) for the fairly lengthy list of valid Types. – indivisible Jun 18 '14 at 07:48
  • ` BigDecimal value = new BigDecimal(1); value = value.divide(new BigDecimal(3));` will fail! Try. – Yan Khonski Feb 14 '19 at 10:27
0

What you are doing in your code is,

multiply an integer x to 100 and then divide the result by integer y. So their output will be integer only.

Then you are storing this int result in a float variable. So it just adds .0 to your result.

To get the result you want you can do any of the following,

1.      int x = 76;
        int y = 266;
        float z = (x * 100.0f) / y;

Note do not write 100.0 because it will be treated as a double number so you will get loss of precision error

2.     float x = 76;
       int y = 266;
       float z = (x * 100) / y;

3.     float x = 76;
       float y = 266;
       float z = (x * 100) / y;
gprathour
  • 14,813
  • 5
  • 66
  • 90