Java Number Minus minus(Number n)

Here you can find the source of minus(Number n)

Description

Returns The negation of a number, regardless of its type.

License

Open Source License

Parameter

Parameter Description
n The number to negate.

Return

Negative the number, or null if the number was null.

Declaration

static public Number minus(Number n) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  ja  v a  2  s  .  c  o m*/
 * The contents of this file are subject to the Open Software License
 * Version 3.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.opensource.org/licenses/osl-3.0.txt
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 */

public class Main {
    /**
     * Returns The negation of a number, regardless of its type.
     * @param n The number to negate.
     * @return Negative the number, or null if the number was null.
     */
    static public Number minus(Number n) {
        if (n instanceof Long)
            return new Long(-n.longValue());
        if (n instanceof Integer)
            return new Integer(-n.intValue());
        if (n instanceof Short)
            return new Short((short) -n.shortValue());
        if (n instanceof Byte)
            return new Byte((byte) -n.byteValue());
        if (n instanceof Double)
            return new Double(-n.doubleValue());
        if (n instanceof Float)
            return new Float(-n.floatValue());
        return null;
    }
}

Related

  1. minus(double[] p, double[] q)
  2. minus(double[][] x, double[][] y, double[][] r)
  3. minus(int flags, int set)
  4. minus(Integer a, Integer b)
  5. minus(Number n)
  6. minus(String bigger, String smaller)
  7. minus(String second, String first)
  8. minus128(int v)
  9. minuscula(char c)