Java Integer Create toInt(double num)

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

Description

Convert a double number into a integer

License

Open Source License

Parameter

Parameter Description
num double value to be converted

Return

converted integer value

Declaration

public static long toInt(double num) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j  av  a2s  .c om
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

public class Main {
    /**
     * <p>Convert a <code>String</code> to an <code>int</code>, returning
     * <code>zero</code> if the conversion fails.</p>
     * <p/>
     * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
     * <p/>
     * <pre>
     *   NumberUtility.toInt(null) = 0
     *   NumberUtility.toInt("")   = 0
     *   NumberUtility.toInt("1")  = 1
     * </pre>
     *
     * @param str the string to convert, may be null
     * @return the int represented by the string, or <code>zero</code> if
     *         conversion fails
     */
    public static int toInt(String str) {
        return toInt(str, 0);
    }

    /**
     * <p>Convert a <code>String</code> to an <code>int</code>, returning a
     * default value if the conversion fails.</p>
     * <p/>
     * <p>If the string is <code>null</code>, the default value is returned.</p>
     * <p/>
     * <pre>
     *   NumberUtility.toInt(null, 1) = 1
     *   NumberUtility.toInt("", 1)   = 1
     *   NumberUtility.toInt("1", 0)  = 1
     * </pre>
     *
     * @param str          the string to convert, may be null
     * @param defaultValue the default value
     * @return the int represented by the string, or the default if conversion fails
     */
    public static int toInt(String str, int defaultValue) {
        if (str == null)
            return defaultValue;

        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * Convert a double number into a integer
     * @param num double value to be converted
     * @return converted integer value
     */
    public static long toInt(double num) {
        return (new Double(num)).intValue();
    }

    /**
     * Convert a float number into a integer
     * @param num float value to be converted
     * @return converted integer value
     */
    public static long toInt(float num) {
        return (new Float(num)).intValue();
    }
}

Related

  1. toInt(double d)
  2. toInt(double d)
  3. toInt(double d)
  4. toInt(double f)
  5. toInt(double in)
  6. toInt(double val)
  7. toInt(double value)
  8. toInt(double[] arr)
  9. toInt(double[] array)