Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6. A naive approach would have me add 2/12 and 6/12 and then reduce. How can I achieve a common denominator with the least performance penalty? W...
I want to compare to variables, both of type T extends Number. Now I want to know which of the two variables is greater than the other or equal. Unfortunately I don't know the exact type yet, I only know that it will be a subtype of java.lang.Number. How can I do that? Thanks! EDIT: I tried another workaround using TreeSets, which actually worked with natural ordering (of course it works, all...
I'm working on a project that requires me to have a string representation of an array. The problem is having this duplicated code, that I'm sure can be refactored in some way, but I haven't found one yet. private static String printDoubleArray(String title, double[] array){ String result = title; for (double d : array) { result += d + " "; } return result; } private st...
I want to convert some numbers which I got as strings into Doubles, but these numbers are not in US standard locale, but in a different one. How can I do that?
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).
This is NOT homework. Part 1 Is it possible to write a generic method, something like this: <T extends Number> T plusOne(T num) { return num + 1; // DOESN'T COMPILE! How to fix??? } Short of using a bunch of instanceof and casts, is this possible? Part 2 The following 3 methods compile: Integer plusOne(Integer num) { return num + 1; } Double plusOne(Double num) { ...
How to sort a List<Number>? Example: List<Number> li = new ArrayList<Number>(); //list of numbers li.add(new Integer(20)); li.add(new Double(12.2)); li.add(new Float(1.2));
In Java, all numeric types extend from java.lang.Number. Would it be a good idea to have a method like the following: public boolean areEqual(Number first, Number second) { if (first != null && second != null) { return first.equals(second); } } I'm concerned about cases where a double 2.00000 does not equal an int 2. Are these handled by the built-in equals? If not, i...
I am trying to create a nullalble object in Java but no idea how to do this , in C# this would be done like this int? someTestInt; This allows me to check for for null , while in certain cases i can use a 0 value ,this isnt always possible since certain execution paths allow 0 values
Possible Duplicate: Wrapper class and == operator Hi when I am comparing Integer with == I have some problem so can you explain me why second test is success too ? @Test public void integerTest() { Integer prvni = 127; Integer druhy = 127; Integer treti = 128; Integer ctvrty = 128; assertTrue(prvni == druhy); assertTrue(treti != ctvrty); }
According to the specification of Number.longValue() the method should... Returns the value of the specified number as a long. This may involve rounding or truncation. However, the BigInteger (and BigDecimal) overrides this method and returns the 64 low bits of the integer part of the number it represents. From the docs of BigInteger for example: [...]if this BigInteger is too big to ...
I need to convert array of Objects into a Long/Integer.. Problem is that those Objects are sometimes BigIntegers, sometimes BigDecimals and sometimes even something else. Is there any good libraries for accomplishing this? for example... for (Object[] o : result) { Long l = SomeClass.convertToLong(o[0]); Integer i = SomeClass.convertToInt(o[1]); }
I'm trying to take the user input, which may or may not have a comma in it, and put a comma in the correct places upon the user deselecting the field (or at all, if that's not possible). I would also like to know how I can subtract the commas to make the number just an integer. Thanks in advance for your help!
I have a bunch of numeric values. They can be Short, Integer, Long, Float or Double (and are the output of an external library (snakeYaml) which returns these as type Object) I'd like to convert these Objects (which are guaranteed to be Numbers) to Double values in my program. (Storage space is not an issue). The Java compiler obviously throws a ClassCastException when attempting to cast from...
Why this works: Object prova = 9.2; System.out.println(prova); Double prova2 = (Double) prova; System.out.println(prova2); And this doesn't? Object prova = 9.2; System.out.println(prova); Float prova2 = (Float) prova; System.out.println(prova2); I lost 1 hour in my javaa android application caause of this thing so i ha to cast it in a double and than the double in a flout or i had an ...
I am parsing XML files and I have several methods similar to: public static Integer getInteger(Object integer) { if (integer == null) { return 0; } try { return Integer.parseInt(integer.toString(), 10); } catch (Exception ex) { return 0; } } So basically, you pass an object in with the assumption of converting it to an Integer (I also have versions for Float, etc). ...
Is there a java class abstraction to denote scientific numbers (e.g. the number, 10 power modulus), so that I can do simple operations like multiplication or division on top of them. Note: Looking for something similar to Fraction here.
Is there a datatype in Java that stores a decimal number more precisely than a double?
Assume that a query result exists called resultSet having a field available as templateId. Also, a map 'templateMap' exists with keys of templatedId. I am not able to get any result from the following, any help appreciated. <c:foreach var="row" items="${resultSet.rows}"> <c:out value="${templateMap[row.templateId]}" /> </c:foreach> Note: this is a coding horror applicat...
Integer extends Number so in that sense Number becomes the superclass of int. I want to store an int array into a Number array.. I have the following code.However, it seems it is not allowed in java. int[] b = {1,2}; Number[] a = b; Why java does not allow me to store an int array in number array and how do I store this out ?
I want to have a method which calculates the mean of a LinkedList of type Integer, Double and Float. The problem is the sum += i; statement, since java says that the + operator isn't defined for type Object. I could do a cast, but if the LinkedList was of type Float, for example, and the cast was to Integer, I would be not computing the correct mean. What should I do? Thanks. public dou...
  /*
   * Copyright 1994-2001 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang;

The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.

Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.

Author(s):
Lee Boynton
Arthur van Hoff
Since:
JDK1.0
See also:
Byte
Double
Float
Integer
Long
Short
 
 public abstract class Number implements java.io.Serializable {
    
Returns the value of the specified number as an int. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type int.
 
     public abstract int intValue();

    
Returns the value of the specified number as a long. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type long.
 
     public abstract long longValue();

    
Returns the value of the specified number as a float. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type float.
 
     public abstract float floatValue();

    
Returns the value of the specified number as a double. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type double.
 
     public abstract double doubleValue();

    
Returns the value of the specified number as a byte. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type byte.
Since:
JDK1.1
 
     public byte byteValue() {
         return (byte)intValue();
     }

    
Returns the value of the specified number as a short. This may involve rounding or truncation.

Returns:
the numeric value represented by this object after conversion to type short.
Since:
JDK1.1
    public short shortValue() {
        return (short)intValue();
    }

    
use serialVersionUID from JDK 1.0.2 for interoperability
    private static final long serialVersionUID = -8742448824652078965L;
New to GrepCode? Check out our FAQ X