Add Two Float Values in Java

Issue with adding Float values In java

Try double instead.

double abc = (double) (5042034.0 + 1425837.2);

Float sum in java

You should use the DecimalFormat class.

Example

DecimalFormat decFormat = new DecimalFormat("#.##");
Log.i("Operation", "sum: " + decFormat.format(a + b));

If you want to ensure that there are always going to be two decimal places, i.e: 2.90 or 2.00, then you can do something like:

DecimalFormat decFormat = new DecimalFormat("0.00");
Log.i("Operation", "sum: " + decFormat.format(a + b));

Method to add two values of type float and integer in Android

For more control, try the basic OOP concept of overloading methods like

public float addition(float numa, float numb) {
// will return float
return numa + numb;
}

public int addition(int numa, float numb) {
// explicitly cast to int
return numa + (int) numb;
}

public int addition(float numa, int numb) {
// explicitly cast to int
return (int) numa + numb;
}

public int addition(int numa, int numb) {
// will return int
return numa + numb;
}

To examin your in put, try something like this...

public void examineInput(String input1, String input2) {

// For both are float
if (input1.indexOf(".") != -1 && input2.indexOf(".") != -1) {
float numa = Float.parseFloat(input1);
float numb = Float.parseFloat(input2);
float ans = addition(numa, numb);
Log.i(TAG, String.format("%f + %f = %f", numa, numb, ans));
}

// for first to be int and second to be float
else if (input1.indexOf(".") == -1 && input2.indexOf(".") != -1) {
int numa = Integer.parseInt(input1);
float numb = Float.parseFloat(input2);
int ans = addition(numa, numb);
Log.i(TAG, String.format("%d + %f = %d", numa, numb, ans));
}

// for first to be float and second to be int
else if (input1.indexOf(".") != -1 && input2.indexOf(".") == -1) {
float numa = Float.parseFloat(input1);
int numb = Integer.parseInt(input2);
int ans = addition(numa, numb);
Log.i(TAG, String.format("%f + %d = %d", numa, numb, ans));
}

// for both to be int
else if (input1.indexOf(".") == -1 && input2.indexOf(".") == -1) {
int numa = Integer.parseInt(input1);
int numb = Integer.parseInt(input2);
int ans = addition(numa, numb);
Log.i(TAG, String.format("%d + %d = %d", numa, numb, ans));
}
}

And the is the input to test this code, with output

examineInput("5.2", "6.2");    // 5.200000 + 6.200000 = 11.400000
examineInput("5", "3.6"); // 5 + 3.600000 = 8
examineInput("1.6", "5"); // 1.600000 + 5 = 6
examineInput("5", "5"); // 5 + 5 = 10

Note: you need to verify that examineInput always get valid numbers, not strings of non numaric characters...

Hope this helps to improve OOP concepts as well..:)

Adding and subtracting exact values to float

Underlying IEEE 754 floating point model has this property: if you re-interpret the bits as Integer, taking the next float after (or before depending on the direction) is just like taking the next (or previous) integer, that is adding or subtracting 1 to the bit pattern re-interpreted as integer.

Stepping n times is adding (or subtracting) n to the bit pattern. It's as simple as that as long as the sign does not change, and you don't overflow to NaN or Inf.

And the number of different floats between two floats is the difference of two integers if the signs agree.

If signs differ, since the float has a sign-magnitude like representation, which does not fit the integer representation, you'll then have to exert a bit of arithmetic.

Why do floats seem to add incorrectly in Java?

Floats are an approximation of the actual number in Java, due to the way they're stored. If you need exact values, use a BigDecimal instead.

How to print a float with 2 decimal places in Java?

You can use the printf method, like so:

System.out.printf("%.2f", val);

In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).

There are other conversion characters you can use besides f:

  • d: decimal integer
  • o: octal integer
  • e: floating-point in scientific notation


Related Topics



Leave a reply



Submit