Skip to content

Commit d1058dc

Browse files
authored
method to parse a float (#754)
1 parent f6b863b commit d1058dc

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/java/com/fasterxml/jackson/core/io/NumberInput.java

+10
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ public static double parseDouble(String s) throws NumberFormatException {
306306
return Double.parseDouble(s);
307307
}
308308

309+
/**
310+
* @param s a string representing a number to parse
311+
* @return closest matching float
312+
* @throws NumberFormatException if string cannot be represented by a float
313+
* @since v2.14
314+
*/
315+
public static float parseFloat(String s) throws NumberFormatException {
316+
return Float.parseFloat(s);
317+
}
318+
309319
public static BigDecimal parseBigDecimal(String s) throws NumberFormatException {
310320
return BigDecimalParser.parse(s);
311321
}

src/test/java/com/fasterxml/jackson/core/io/TestNumberInput.java

+7
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@ public void testNastySmallDouble()
1010
final String nastySmallDouble = "2.2250738585072012e-308";
1111
assertEquals(Double.parseDouble(nastySmallDouble), NumberInput.parseDouble(nastySmallDouble));
1212
}
13+
14+
public void testParseFloat()
15+
{
16+
final String exampleFloat = "1.199999988079071";
17+
assertEquals(1.1999999f, NumberInput.parseFloat(exampleFloat));
18+
assertEquals(1.2f, (float)NumberInput.parseDouble(exampleFloat));
19+
}
1320
}
1421

0 commit comments

Comments
 (0)