Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two java.lang.Numbers?

Tags:

java

I have two Numbers. Eg:

Number a = 2; Number b = 3; //Following is an error: Number c = a + b; 

Why arithmetic operations are not supported on Numbers? Anyway how would I add these two numbers in java? (Of course I'm getting them from somewhere and I don't know if they are Integer or float etc).

like image 924
amit Avatar asked Apr 27 '10 13:04

amit


People also ask

How do you sum numbers in Java?

So you simply make this: sum=sum+num; for the cycle. For example sum is 0, then you add 5 and it becomes sum=0+5 , then you add 6 and it becomes sum = 5 + 6 and so on.

How do you add three numbers in Java?

import java. util. Scanner; //used for question 2 public class firstassignment { public static void main(String[] args) { // TODO Auto-generated method stub //question 1: the Largest Number int num1 = 10; int num2 = 5; int num3 = 20; if( num1 >= num2 & num1 >= num3) System.


2 Answers

You say you don't know if your numbers are integer or float... when you use the Number class, the compiler also doesn't know if your numbers are integers, floats or some other thing. As a result, the basic math operators like + and - don't work; the computer wouldn't know how to handle the values.

START EDIT

Based on the discussion, I thought an example might help. Computers store floating point numbers as two parts, a coefficient and an exponent. So, in a theoretical system, 001110 might be broken up as 0011 10, or 32 = 9. But positive integers store numbers as binary, so 001110 could also mean 2 + 4 + 8 = 14. When you use the class Number, you're telling the computer you don't know if the number is a float or an int or what, so it knows it has 001110 but it doesn't know if that means 9 or 14 or some other value.

END EDIT

What you can do is make a little assumption and convert to one of the types to do the math. So you could have

Number c = a.intValue() + b.intValue(); 

which you might as well turn into

Integer c = a.intValue() + b.intValue(); 

if you're willing to suffer some rounding error, or

Float c = a.floatValue() + b.floatValue(); 

if you suspect that you're not dealing with integers and are okay with possible minor precision issues. Or, if you'd rather take a small performance blow instead of that error,

BigDecimal c = new BigDecimal(a.floatValue()).add(new BigDecimal(b.floatValue())); 
like image 139
Pops Avatar answered Sep 21 '22 10:09

Pops


It would also work to make a method to handle the adding for you. Now I do not know the performance impact this will cause but I assume it will be less than using BigDecimal.

public static Number addNumbers(Number a, Number b) {     if(a instanceof Double || b instanceof Double) {         return a.doubleValue() + b.doubleValue();     } else if(a instanceof Float || b instanceof Float) {         return a.floatValue() + b.floatValue();     } else if(a instanceof Long || b instanceof Long) {         return a.longValue() + b.longValue();     } else {         return a.intValue() + b.intValue();     } } 
like image 37
SkidRunner Avatar answered Sep 22 '22 10:09

SkidRunner