java.lang.Math.round()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中java.lang.Math.round()方法的一些代码示例,展示了Math.round()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.round()方法的具体详情如下:
包路径:java.lang.Math
类名称:Math
方法名:round

Math.round介绍

[英]Returns the result of rounding the argument to an integer. The result is equivalent to (long) Math.floor(d+0.5).

Special cases:

  • round(+0.0) = +0.0
  • round(-0.0) = +0.0
  • round((anything > Long.MAX_VALUE) = Long.MAX_VALUE
  • round((anything < Long.MIN_VALUE) = Long.MIN_VALUE
  • round(+infinity) = Long.MAX_VALUE
  • round(-infinity) = Long.MIN_VALUE
  • round(NaN) = +0.0
    [中]返回将参数舍入为整数的结果。结果相当于(长)数学。地板(d+0.5)。
    特殊情况:
    *四舍五入(+0.0)=+0.0
    *四舍五入(-0.0)=+0.0
    *四舍五入((任意项>长。最大值)=长。最大值
    *圆形((任何<Long.MIN_值)=长。最小值
    *圆形(+无限)=长。最大值
    *圆形(-无穷大)=长。最小值
    *圆形(NaN)=+0.0

代码示例

代码示例来源:origin: PhilJay/MPAndroidChart

@Override
public String getFormattedValue(float value) {
  int index = Math.round(value);
  if (index < 0 || index >= mValueCount || index != (int)value)
    return "";
  return mValues[index];
}

代码示例来源:origin: stackoverflow.com

value = 5.5

Math.floor(value) //  5
Math.ceil(value)  //  6
Math.round(value) //  6
Math.trunc(value) //  5
parseInt(value)   //  5
~~value           //  5
value | 0         //  5
value >> 0        //  5
value >>> 0       //  5
value - value % 1 //  5

代码示例来源:origin: stackoverflow.com

value = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

Math.floor(value) // -900719925474100
Math.ceil(value)  // -900719925474099
Math.round(value) // -900719925474099
Math.trunc(value) // -900719925474099
parseInt(value)   // -900719925474099
value | 0         // -858993459
~~value           // -858993459
value >> 0        // -858993459
value >>> 0       //  3435973837
value - value % 1 // -900719925474099

代码示例来源:origin: stackoverflow.com

value = -5.5

Math.floor(value) // -6
Math.ceil(value)  // -5
Math.round(value) // -5
Math.trunc(value) // -5
parseInt(value)   // -5
value | 0         // -5
~~value           // -5
value >> 0        // -5
value >>> 0       // 4294967291
value - value % 1 // -5

代码示例来源:origin: stackoverflow.com

var intvalue = Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue ); 
var intvalue = Math.round( floatvalue );

// `Math.trunc` was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue );

代码示例来源:origin: stackoverflow.com

value = Number.MAX_SAFE_INTEGER/10 // 900719925474099.1

Math.floor(value) //  900719925474099
Math.ceil(value)  //  900719925474100
Math.round(value) //  900719925474099
Math.trunc(value) //  900719925474099
parseInt(value)   //  900719925474099
value | 0         //  858993459
~~value           //  858993459
value >> 0        //  858993459
value >>> 0       //  858993459
value - value % 1 //  900719925474099

代码示例来源:origin: stackoverflow.com

public static double round(double value, int places) {
  if (places < 0) throw new IllegalArgumentException();

  long factor = (long) Math.pow(10, places);
  value = value * factor;
  long tmp = Math.round(value);
  return (double) tmp / factor;
}

代码示例来源:origin: google/guava

/**
 * Computes the optimal k (number of hashes per element inserted in Bloom filter), given the
 * expected insertions and total number of bits in the Bloom filter.
 *
 * <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula.
 *
 * @param n expected insertions (must be positive)
 * @param m total number of bits in Bloom filter (must be positive)
 */
@VisibleForTesting
static int optimalNumOfHashFunctions(long n, long m) {
 // (m / n) * log(2), but avoid truncation due to division!
 return Math.max(1, (int) Math.round((double) m / n * Math.log(2)));
}

代码示例来源:origin: libgdx/libgdx

private void storeKerningOffset (int firstGlyphCode, int secondGlyphCode, int offset) {
  // Scale the offset values using the font size.
  int value = Math.round(offset * scale);
  if (value == 0) {
    return;
  }
  int key = (firstGlyphCode << 16) | secondGlyphCode;
  kernings.put(key, value);
}

代码示例来源:origin: libgdx/libgdx

private void storeKerningOffset (int firstGlyphCode, int secondGlyphCode, int offset) {
  // Scale the offset values using the font size.
  int value = Math.round(offset * scale);
  if (value == 0) {
    return;
  }
  int key = (firstGlyphCode << 16) | secondGlyphCode;
  kernings.put(key, value);
}

代码示例来源:origin: libgdx/libgdx

@Override
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
  Vector2 scaled = scaling.apply(getWorldWidth(), getWorldHeight(), screenWidth, screenHeight);
  int viewportWidth = Math.round(scaled.x);
  int viewportHeight = Math.round(scaled.y);
  // Center.
  setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight);
  apply(centerCamera);
}

代码示例来源:origin: libgdx/libgdx

@Override
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
  Vector2 scaled = scaling.apply(getWorldWidth(), getWorldHeight(), screenWidth, screenHeight);
  int viewportWidth = Math.round(scaled.x);
  int viewportHeight = Math.round(scaled.y);
  // Center.
  setScreenBounds((screenWidth - viewportWidth) / 2, (screenHeight - viewportHeight) / 2, viewportWidth, viewportHeight);
  apply(centerCamera);
}

代码示例来源:origin: libgdx/libgdx

protected void setDisplayMode (int width, int height) {
  Dimension size = new Dimension(Math.round(width / scaleX), Math.round(height / scaleY));
  LwjglFrame.this.getContentPane().setPreferredSize(size);
  LwjglFrame.this.getContentPane().invalidate();
  LwjglFrame.this.pack();
  LwjglFrame.this.setLocationRelativeTo(null);
  updateSize(width, height);
}

代码示例来源:origin: libgdx/libgdx

protected void setDisplayMode (int width, int height) {
  Dimension size = new Dimension(Math.round(width / scaleX), Math.round(height / scaleY));
  LwjglFrame.this.getContentPane().setPreferredSize(size);
  LwjglFrame.this.getContentPane().invalidate();
  LwjglFrame.this.pack();
  LwjglFrame.this.setLocationRelativeTo(null);
  updateSize(width, height);
}

代码示例来源:origin: libgdx/libgdx

/** Centers the dialog in the stage and calls {@link #show(Stage, Action)} with a {@link Actions#fadeIn(float, Interpolation)}
 * action. */
public Dialog show (Stage stage) {
  show(stage, sequence(Actions.alpha(0), Actions.fadeIn(0.4f, Interpolation.fade)));
  setPosition(Math.round((stage.getWidth() - getWidth()) / 2), Math.round((stage.getHeight() - getHeight()) / 2));
  return this;
}

代码示例来源:origin: libgdx/libgdx

/** Centers the dialog in the stage and calls {@link #show(Stage, Action)} with a {@link Actions#fadeIn(float, Interpolation)}
 * action. */
public Dialog show (Stage stage) {
  show(stage, sequence(Actions.alpha(0), Actions.fadeIn(0.4f, Interpolation.fade)));
  setPosition(Math.round((stage.getWidth() - getWidth()) / 2), Math.round((stage.getHeight() - getHeight()) / 2));
  return this;
}

代码示例来源:origin: libgdx/libgdx

/** Kindly borrowed from PlayN. **/
protected int getRelativeY (NativeEvent e, CanvasElement target) {
  float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
  return Math.round(yScaleRatio
    * (e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop()));
}

代码示例来源:origin: libgdx/libgdx

/** Kindly borrowed from PlayN. **/
protected int getRelativeX (NativeEvent e, CanvasElement target) {
  float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
  return Math.round(xScaleRatio
    * (e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft()));
}

代码示例来源:origin: libgdx/libgdx

/** Kindly borrowed from PlayN. **/
protected int getRelativeX (NativeEvent e, CanvasElement target) {
  float xScaleRatio = target.getWidth() * 1f / target.getClientWidth(); // Correct for canvas CSS scaling
  return Math.round(xScaleRatio
    * (e.getClientX() - target.getAbsoluteLeft() + target.getScrollLeft() + target.getOwnerDocument().getScrollLeft()));
}

代码示例来源:origin: libgdx/libgdx

/** Kindly borrowed from PlayN. **/
protected int getRelativeY (NativeEvent e, CanvasElement target) {
  float yScaleRatio = target.getHeight() * 1f / target.getClientHeight(); // Correct for canvas CSS scaling
  return Math.round(yScaleRatio
    * (e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop()));
}

相关文章