Skip to content

Commit

Permalink
Use Number.prototype.toPrecision in Platform.formatCompact4Digits.
Browse files Browse the repository at this point in the history
This is a simpler and also J2Wasm compatible implementation.

RELNOTES=n/a
PiperOrigin-RevId: 637629097
  • Loading branch information
gkdn authored and Google Java Core Libraries committed May 27, 2024
1 parent 3f61870 commit c4b883d
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.google.common.base;

import static jsinterop.annotations.JsPackage.GLOBAL;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;
import jsinterop.annotations.JsPackage;
import org.checkerframework.checker.nullness.qual.Nullable;

/** @author Jesse Wilson */
Expand All @@ -33,9 +31,12 @@ static CharMatcher precomputeCharMatcher(CharMatcher matcher) {
}

static String formatCompact4Digits(double value) {
return "" + ((Number) (Object) value).toPrecision(4);
return toPrecision(value, 4);
}

@JsMethod(name = "Number.prototype.toPrecision.call", namespace = JsPackage.GLOBAL)
private static native String toPrecision(double value, int precision);

@JsMethod
static native boolean stringIsNullOrEmpty(@Nullable String string) /*-{
return !string;
Expand All @@ -51,11 +52,6 @@ static native String emptyToNull(@Nullable String string) /*-{
return string || null;
}-*/;

@JsType(isNative = true, name = "number", namespace = GLOBAL)
private interface Number {
double toPrecision(int precision);
}

static CommonPattern compilePattern(String pattern) {
throw new UnsupportedOperationException();
}
Expand Down

0 comments on commit c4b883d

Please sign in to comment.