Java Char Create toCharWithoutOverflow(double value)

Here you can find the source of toCharWithoutOverflow(double value)

Description

Like Math#toIntExact(long) but for char range.

License

Apache License

Declaration

public static char toCharWithoutOverflow(double value) 

Method Source Code

//package com.java2s;
/*//from  w w  w  .j  a  va 2 s .  c  om
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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 {
    /**
     * Like {@link Math#toIntExact(long)} but for char range.
     */
    public static char toCharWithoutOverflow(float value) {
        if (value < Character.MIN_VALUE || value > Character.MAX_VALUE) {
            throw new ArithmeticException("char overflow");
        }
        return (char) value;
    }

    /**
     * Like {@link Math#toIntExact(long)} but for char range.
     */
    public static char toCharWithoutOverflow(double value) {
        if (value < Character.MIN_VALUE || value > Character.MAX_VALUE) {
            throw new ArithmeticException("char overflow");
        }
        return (char) value;
    }
}

Related

  1. toChars(int[] src, int srcOff, int srcLen, char[] dest, int destOff)
  2. toChars(String from)
  3. toChars(String s)
  4. toCharSequence(final Object value)
  5. toCharString(byte[] bytes)