Take the integer part of a number and put it in [0, 255] That is to say if integer<0, return 0. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Take the integer part of a number and put it in [0, 255] That is to say if integer<0, return 0.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double number = 2.45678;
        System.out.println(limite0_255(number));
    }/*from   w w w .  j av  a2 s .co  m*/

    /**
     * Take the integer part of a number and put it in [0, 255]<br>
     * That is to say if integer<0, return 0. If integer>255, return 2555. Return the integer on other case
     * 
     * @param number
     *           Number to limit
     * @return Limited value
     */
    public static int limite0_255(final double number) {
        final int integer = (int) number;
        if (integer < 0) {
            return 0;
        }
        if (integer > 255) {
            return 255;
        }
        return integer;
    }
}

Related Tutorials