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