Random and Roundup : Float « Date Type « Android

Random and Roundup


//package com.akjava.lib.android.math;

import java.math.BigDecimal;

public final class MathUtils {
   * these code come from jme
  public static final float PI = (float) Math.PI;
  public static final float DEG_TO_RAD = PI / 180.0f;
  public static final float RAD_TO_DEG = 180.0f / PI;

  public static float radToDeg(final float rad) {
    return RAD_TO_DEG * rad;

  public static float degToRad(final float deg) {
    return DEG_TO_RAD * deg;

  public static float random(final float min, final float max) {
    return (float) Math.random() * (max - min) + min;

  public static int randomInt(final int min, final int max) {
    return (int) (Math.random() * (max - min) + min);

  public static float toRoundHalfUp(float value, int index) {
    BigDecimal ret = new BigDecimal(value);
    try {

      return ret.setScale(index, BigDecimal.ROUND_HALF_UP).floatValue();
    } catch (Exception e) {

    return 0;// ret direct



Related examples in the same category

1.Convert a float to 16.16 fixed-point representation
2.Convert a 16.16 fixed-point value to floating point
3.Is Long or double
4.calculate NormDiff
5.DecimalFormat, Maximum Fraction Digits
6.Format Distance
7.Gives the fractional part of a number
8.Close to a value
9.Value equals in a range, and ensure value inside a range
10.scale, round Number
11.Returns the smallest power of two that is greater than