Here you can find the source of toInt(double num)
Parameter | Description |
---|---|
num | double value to be converted |
public static long toInt(double num)
//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(); } }