Return a long as a string but capped as a 32-bit signed value. - Java java.lang

Java examples for java.lang:Math Value

Description

Return a long as a string but capped as a 32-bit signed value.

Demo Code

/*/* w  w  w  .j ava 2  s .  c  o  m*/
 * Copyright (c) 2014. Real Time Genomics Limited.
 *
 * Use of this source code is bound by the Real Time Genomics Limited Software Licence Agreement
 * for Academic Non-commercial Research Purposes only.
 *
 * If you did not receive a license accompanying this file, a copy must first be obtained by email
 * from support@realtimegenomics.com.  On downloading, using and/or continuing to use this source
 * code you accept the terms of that license agreement and any amendments to those terms that may
 * be made from time to time by Real Time Genomics Limited.
 */
//package com.java2s;

public class Main {
    private static final String OVER = String.valueOf(Integer.MAX_VALUE);
    private static final String UNDER = String.valueOf(-Integer.MAX_VALUE);

    /**
     * Return a long as a string but capped as a 32-bit signed value.
     * (e.g. for compatibility with BCF).  Note 0x80000000 is reserved
     * in BCF for missing value.
     * @param val value
     * @return string representation
     */
    public static String cappedInt(final long val) {
        if (val >= Integer.MAX_VALUE) {
            return OVER;
        } else if (val <= -Integer.MIN_VALUE) {
            return UNDER;
        }
        return Long.toString(val);
    }
}

Related Tutorials