Java Integer Create toInt(double d)

Here you can find the source of toInt(double d)

Description

Cast double values into integer.

License

Apache License

Parameter

Parameter Description
d the double value to be casted

Return

the integer

Declaration

public static int toInt(double d) 

Method Source Code

//package com.java2s;
/**//from  w  w w.  j  a  va  2s  .  c o m
 * CastUtil.java
 *
 * Copyright 2014 the original author or authors.
 *
 * We licenses this file to you under the Apache License, version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Cast double values into integer.
     *
     * @param d the double value to be casted
     * @return the integer
     */
    public static int toInt(double d) {
        if (d > Integer.MAX_VALUE || d < Integer.MIN_VALUE) {
            throw new IllegalArgumentException("Value out of int range - " + d);
        }

        return (int) d;
    }

    /**
     * Cast long values into integer.
     *
     * @param d the long value to be casted
     * @return the integer
     */
    public static int toInt(long d) {
        if (d > Integer.MAX_VALUE || d < Integer.MIN_VALUE) {
            throw new IllegalArgumentException("Value out of int range - " + d);
        }

        return (int) d;
    }
}

Related

  1. toInt(byte[] value)
  2. toInt(byte[] value)
  3. toInt(char msc, char lsc)
  4. toInt(char[] bytes, boolean le)
  5. toInt(double d)
  6. toInt(double d)
  7. toInt(double f)
  8. toInt(double in)
  9. toInt(double num)