¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | /** |
| | | * æ°åç±»åå·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class NumberUtil extends org.springframework.util.NumberUtils { |
| | | |
| | | //----------------------------------------------------------------------- |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>int</code>, returning |
| | | * <code>zero</code> if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toInt(null) = 0 |
| | | * NumberUtil.toInt("") = 0 |
| | | * NumberUtil.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(final String str) { |
| | | return toInt(str, -1); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to an <code>int</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toInt(null, 1) = 1 |
| | | * NumberUtil.toInt("", 1) = 1 |
| | | * NumberUtil.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(@Nullable final String str, final int defaultValue) { |
| | | if (str == null) { |
| | | return defaultValue; |
| | | } |
| | | try { |
| | | return Integer.valueOf(str); |
| | | } catch (final NumberFormatException nfe) { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>long</code>, returning |
| | | * <code>zero</code> if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toLong(null) = 0L |
| | | * NumberUtil.toLong("") = 0L |
| | | * NumberUtil.toLong("1") = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @return the long represented by the string, or <code>0</code> if |
| | | * conversion fails |
| | | */ |
| | | public static long toLong(final String str) { |
| | | return toLong(str, 0L); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>long</code>, returning a |
| | | * default value if the conversion fails.</p> |
| | | * |
| | | * <p>If the string is <code>null</code>, the default value is returned.</p> |
| | | * |
| | | * <pre> |
| | | * NumberUtil.toLong(null, 1L) = 1L |
| | | * NumberUtil.toLong("", 1L) = 1L |
| | | * NumberUtil.toLong("1", 0L) = 1L |
| | | * </pre> |
| | | * |
| | | * @param str the string to convert, may be null |
| | | * @param defaultValue the default value |
| | | * @return the long represented by the string, or the default if conversion fails |
| | | */ |
| | | public static long toLong(@Nullable final String str, final long defaultValue) { |
| | | if (str == null) { |
| | | return defaultValue; |
| | | } |
| | | try { |
| | | return Long.valueOf(str); |
| | | } catch (final NumberFormatException nfe) { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @return double value |
| | | */ |
| | | public static Double toDouble(String value) { |
| | | return toDouble(value, null); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return double value |
| | | */ |
| | | public static Double toDouble(@Nullable String value, Double defaultValue) { |
| | | if (value != null) { |
| | | return Double.valueOf(value.trim()); |
| | | } |
| | | return defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @return double value |
| | | */ |
| | | public static Float toFloat(String value) { |
| | | return toFloat(value, null); |
| | | } |
| | | |
| | | /** |
| | | * <p>Convert a <code>String</code> to a <code>Double</code> |
| | | * |
| | | * @param value value |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return double value |
| | | */ |
| | | public static Float toFloat(@Nullable String value, Float defaultValue) { |
| | | if (value != null) { |
| | | return Float.valueOf(value.trim()); |
| | | } |
| | | return defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * All possible chars for representing a number as a String |
| | | */ |
| | | private final static char[] DIGITS = { |
| | | '0', '1', '2', '3', '4', '5', |
| | | '6', '7', '8', '9', 'a', 'b', |
| | | 'c', 'd', 'e', 'f', 'g', 'h', |
| | | 'i', 'j', 'k', 'l', 'm', 'n', |
| | | 'o', 'p', 'q', 'r', 's', 't', |
| | | 'u', 'v', 'w', 'x', 'y', 'z', |
| | | 'A', 'B', 'C', 'D', 'E', 'F', |
| | | 'G', 'H', 'I', 'J', 'K', 'L', |
| | | 'M', 'N', 'O', 'P', 'Q', 'R', |
| | | 'S', 'T', 'U', 'V', 'W', 'X', |
| | | 'Y', 'Z' |
| | | }; |
| | | |
| | | /** |
| | | * å° long 转çå符串 为 62 è¿å¶ |
| | | * |
| | | * @param i æ°å |
| | | * @return çå符串 |
| | | */ |
| | | public static String to62String(long i) { |
| | | int radix = DIGITS.length; |
| | | char[] buf = new char[65]; |
| | | int charPos = 64; |
| | | i = -i; |
| | | while (i <= -radix) { |
| | | buf[charPos--] = DIGITS[(int) (-(i % radix))]; |
| | | i = i / radix; |
| | | } |
| | | buf[charPos] = DIGITS[(int) (-i)]; |
| | | |
| | | return new String(buf, charPos, (65 - charPos)); |
| | | } |
| | | |
| | | } |