Skip to content

Commit cc05493

Browse files
committed
Fixed #707
1 parent a69bd56 commit cc05493

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.4.6 (not yet released)
8+
9+
#707: Error in getting string representation of an ObjectNode with a float number value
10+
(reported by @navidqar)
11+
712
2.4.5 (13-Jan-2015)
813

914
#635: Reduce cachability of `Map` deserializers, to avoid problems with per-property config changes

src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public BigInteger bigIntegerValue() {
8989

9090
@Override
9191
public String asText() {
92-
return NumberOutput.toString(_value);
92+
// As per [jackson-core#179]
93+
// return NumberOutput.toString(_value);
94+
return Float.toString(_value);
9395
}
9496

9597
@Override

src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,25 @@ public void testDouble() throws Exception
128128
// @since 2.2
129129
public void testFloat()
130130
{
131-
FloatNode n = FloatNode.valueOf(0.25f);
131+
FloatNode n = FloatNode.valueOf(0.45f);
132132
assertStandardEquals(n);
133133
assertTrue(0 != n.hashCode());
134134
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, n.asToken());
135135
assertEquals(JsonParser.NumberType.FLOAT, n.numberType());
136136
assertEquals(0, n.intValue());
137-
assertEquals(0.25, n.doubleValue());
138-
assertEquals(0.25f, n.floatValue());
137+
138+
// NOTE: conversion to double NOT as simple as with exact numbers like 0.25:
139+
assertEquals(0.45f, n.floatValue());
140+
assertEquals("0.45", n.asText());
141+
142+
// so; as double we'll get more complex number; however, should round-trip
143+
// to something that gets printed the same way. But not exact value, alas, hence:
144+
assertEquals("0.45", String.valueOf((float) n.doubleValue()));
145+
139146
assertNotNull(n.decimalValue());
147+
// possibly surprisingly, however, this will produce same output:
140148
assertEquals(BigInteger.ZERO, n.bigIntegerValue());
141-
assertEquals("0.25", n.asText());
149+
assertEquals("0.45", n.asText());
142150

143151
// 1.6:
144152
assertNodeNumbers(FloatNode.valueOf(4.5f), 4, 4.5f);

0 commit comments

Comments
 (0)