yunussasmaz / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Problem when deserializing Float but not with Double #273

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
{{{
package gson.floating.point;

import com.google.gson.Gson;

public class TestFloat {

    public static class FloatData{
        Float cantidad;

        protected FloatData(){}

        public Float getCantidad() {
            return cantidad;
        }
        public void setCantidad(Float cantidad) {
            this.cantidad = cantidad;
        }

    }
    public static class DoubleData{
        Double cantidad;

        protected DoubleData(){}

        public Double getCantidad() {
            return cantidad;
        }
        public void setCantidad(Double cantidad) {
            this.cantidad = cantidad;
        }

    }

    public static void main(String[] args) {
        String json="{\"cantidad\":13113715.22}";
        DoubleData d=new Gson().fromJson(json, DoubleData.class);
        FloatData f=new Gson().fromJson(json, FloatData.class);
        //Prints 'Double value: 1.311371522E7'
        System.out.println("Double value: "+d.getCantidad());
        //Prints 'Float value: 1.3113715E7', decimal lost
        System.out.println("Float value: "+f.getCantidad());
        assert d.getCantidad().equals(13113715.22);
        assert f.getCantidad().equals(13113715.22);
    }

}
}}}
What is the expected output? Number with decimals 
What do you see instead? Number without decimals
What version of the product are you using?
Gson 1.4
On what operating system?
Ubuntu 10.10

Original issue reported on code.google.com by cirovlad...@gmail.com on 16 Dec 2010 at 9:59

GoogleCodeExporter commented 9 years ago
Not a gson issue.
All numbers between 13113714.5 and 13113715.5 are represented as 1.3113715E7f.
(13113714.5 falls between 2^23 and 2^24, and float has only 23bit mantissa)

Original comment by kulik.st...@gmail.com on 7 Jan 2011 at 1:43

GoogleCodeExporter commented 9 years ago
Ok, Thanks for the correction.
I have troubles determining the range of float and the java documentation 
doesn't help me.

Original comment by cirovlad...@gmail.com on 7 Jan 2011 at 8:45

GoogleCodeExporter commented 9 years ago

Original comment by joel.leitch@gmail.com on 15 Feb 2011 at 6:57