本文整理了Java中java.lang.Math.round()
方法的一些代码示例,展示了Math.round()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.round()
方法的具体详情如下:
包路径:java.lang.Math
类名称: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:
代码示例来源: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()));
}
内容来源于网络,如有侵权,请联系作者删除!