Answer by Martin Klosi for How to nicely format floating numbers to string...
Here is an answer that actually works (combination of different answers here)public static String removeTrailingZeros(double f){ if(f == (int)f) { return String.format("%d", (int)f); } return...
View ArticleAnswer by android developer for How to nicely format floating numbers to...
Here are two ways to achieve it. First, the shorter (and probably better) way:public static String formatFloatToString(final float f){ final int i = (int)f; if(f == i) return Integer.toString(i);...
View ArticleAnswer by kamal for How to nicely format floating numbers to string without...
String s = String.valueof("your int variable");while (g.endsWith("0") && g.contains(".")) { g = g.substring(0, g.length() - 1); if (g.endsWith(".")) { g = g.substring(0, g.length() - 1); }}
View ArticleAnswer by JasonD for How to nicely format floating numbers to string without...
If the idea is to print integers stored as doubles as if they are integers, and otherwise print the doubles with the minimum necessary precision:public static String fmt(double d){ if(d == (long) d)...
View ArticleAnswer by Hiep for How to nicely format floating numbers to string without...
I made a DoubleFormatter to efficiently convert a great numbers of double values to a nice/presentable string:double horribleNumber = 3598945.141658554548844;DoubleFormatter df = new DoubleFormatter(4,...
View ArticleAnswer by sethu for How to nicely format floating numbers to string without...
The best way to do this is as below:public class Test { public static void main(String args[]){ System.out.println(String.format("%s something", new Double(3.456)));...
View ArticleAnswer by Kadi for How to nicely format floating numbers to string without...
String s = "1.210000";while (s.endsWith("0")){ s = (s.substring(0, s.length() - 1));}This will make the string to drop the tailing 0-s.
View ArticleAnswer by Jeremy Slade for How to nicely format floating numbers to string...
String.format("%.2f", value);
View ArticleAnswer by Pyrolistical for How to nicely format floating numbers to string...
Naw, never mind. The performance loss due to string manipulation is zero.And here's the code to trim the end after %f:private static String trimTrailingZeros(String number) { if(!number.contains("."))...
View ArticleHow to nicely format floating numbers to string without unnecessary decimal 0's
A 64-bit double can represent integer +/- 253 exactly.Given this fact, I choose to use a double type as a single type for all my types, since my largest integer is an unsigned 32-bit number.But now I...
View Article