Returns the number of decimal places needed to display a number, or -2 if exponential notation should be used.
(double n)
| 220 | /** Returns the number of decimal places needed to display a |
| 221 | number, or -2 if exponential notation should be used. */ |
| 222 | public static int getDecimalPlaces(double n) { |
| 223 | if ((int)n==n || Double.isNaN(n)) |
| 224 | return 0; |
| 225 | String s = ""+n; |
| 226 | if (s.contains("E")) |
| 227 | return -2; |
| 228 | while (s.endsWith("0")) |
| 229 | s = s.substring(0,s.length()-1); |
| 230 | int index = s.indexOf("."); |
| 231 | if (index==-1) return 0; |
| 232 | int digits = s.length() - index - 1; |
| 233 | if (digits>4) digits=4; |
| 234 | return digits; |
| 235 | } |
| 236 | |
| 237 | /** Returns the number of decimal places needed to display two numbers, |
| 238 | or -2 if exponential notation should be used. */ |
no test coverage detected