Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace number formatting code #655

Open
drewnoakes opened this issue Feb 6, 2024 · 0 comments
Open

Replace number formatting code #655

drewnoakes opened this issue Feb 6, 2024 · 0 comments

Comments

@drewnoakes
Copy link
Owner

This number formatting method seems a bit wild.

@NotNull
public static String formatDoubleAsString(double value, int precision, boolean zeroes)
{
if (precision < 1)
return "" + Math.round(value);
long intPart = Math.abs((long)value);
long rest = (int)Math.round((Math.abs(value) - intPart) * Math.pow(10, precision));
long restKept = rest;
String res = "";
byte cour;
for (int i = precision; i > 0; i--) {
cour = (byte)(Math.abs(rest % 10));
rest /= 10;
if (res.length() > 0 || zeroes || cour != 0 || i == 1)
res = cour + res;
}
intPart += rest;
boolean isNegative = ((value < 0) && (intPart != 0 || restKept != 0));
return (isNegative ? "-" : "") + intPart + "." + res;
}

In the .NET library, the equivalent code was standing out as a performance issue in traces.

drewnoakes/metadata-extractor-dotnet#403

We should find a suitable replacement. Ideally something built-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant