Returns the closest float value to the real number in the string. @param s the String that will be parsed to a floating point @return the float closest to the real number @exception NumberFormatException if the String doesn't represent a float
(String s)
| 284 | * if the String doesn't represent a float |
| 285 | */ |
| 286 | public static float parseFloat(String s) { |
| 287 | s = s.trim(); |
| 288 | int length = s.length(); |
| 289 | |
| 290 | if (length == 0) { |
| 291 | throw invalidReal(s, false); |
| 292 | } |
| 293 | |
| 294 | // See if this could be a named float |
| 295 | char last = s.charAt(length - 1); |
| 296 | if (last == 'y' || last == 'N') { |
| 297 | return parseName(s, false); |
| 298 | } |
| 299 | |
| 300 | // See if it could be a hexadecimal representation |
| 301 | // We don't use startsWith because there might be a leading sign. |
| 302 | /*if (s.indexOf("0x") != -1 || s.indexOf("0X") != -1) { |
| 303 | return HexStringParser.parseFloat(s); |
| 304 | }*/ |
| 305 | |
| 306 | StringExponentPair info = initialParse(s, length, false); |
| 307 | if (info.infinity || info.zero) { |
| 308 | return info.specialValue(); |
| 309 | } |
| 310 | float result = parseFltImpl(info.s, (int) info.e); |
| 311 | if (Float.floatToIntBits(result) == 0xffffffff) { |
| 312 | throw invalidReal(s, false); |
| 313 | } |
| 314 | return info.negative ? -result : result; |
| 315 | } |
| 316 | } |
no test coverage detected