DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Float Precision Changes When Unexpectedly Up-Casting to Double

Java Float Precision Changes When Unexpectedly Up-Casting to Double

A quick reminder: Remember to match your variable types when working with object containers. Here's an example of values gone wild when upcasting.

Mick Knutson user avatar by
Mick Knutson
·
Jun. 09, 17 · Java Zone · Tutorial
Like (6)
Save
Tweet
23.34K Views

Join the DZone community and get the full member experience.

Join For Free

I ran across a little gotcha today where a float value being inserted into another object container (JSONObject) was not holding the precision of the original value. The JSONObject actually takes a double, not a float, and I overlooked the up-casting initially until I started unit testing. (Have to love unit tests!)

Here is a print of the values I started with:

07:46:15.108 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - ===== abv:: calculateAbv =====
07:46:15.117 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - volume: 16
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - abv: 3.2
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - price: 5.99
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - value_r: 0.117
07:46:15.118 [http-apr-8080-exec-49] INFO c.b.javaee6.service.AbvResource - score_r: 12.0


Here is how I was creating my JSONObject:

JSONObject json = new JSONObject();
json.put("abv_r", valueObject.getAbv());
json.put("volume_r", valueObject.getVolume());
json.put("price_r", valueObject.getPrice());
json.put("value_r", valueObject.getValue());
json.put("score", valueObject.getScore());
result = json.toString();


And the result:

 {
   "abv_r": 3.200000047683716,
   "volume_r": 16,
   "price_r": 5.989999771118164,
   "value_r": 0.11699999868869781,
   "score": 12
 }


The JSONObject.put method does not take a float, but takes a double:

/**
* Put a key/double pair in the JSONObject.
*
* @param key A key string.
* @param value A double which is the value.
* @return this.
* @throws JSONException If the key is null or if the number is invalid.
*/
public JSONObject put(String key, double value)
throws JSONException {
    put(key, new Double(value));
    return this;
}


See org.codehaus.jettison.json.JSONObject for more details.

Now when I cast the values to Strings such as...

“”+valueObject.getAbv()

... the precision of the values are not cast to doubles:

JSONObject json = new JSONObject();
json.put("abv_r", ""+valueObject.getAbv());
json.put("volume_r", ""+valueObject.getVolume());
json.put("price_r", ""+valueObject.getPrice());
json.put("value_r", ""+valueObject.getValue());
json.put("score", ""+valueObject.getScore());
result = json.toString();


And the result:

{
    "abv_r": "3.2",
    "volume_r": "16"",
    "price_r": "5.99",
    "value_r": "0.117"",
    "score": "12.0""
}


So care needs to be taken to remember that other Objects can cast your objects to have undesirable effects.

Another solution is to ensure the valueObject uses doubles. That way, no casting is performed when putting the values into the JSONObject.

Data Types Precision (computer science) Java (programming language)

Published at DZone with permission of Mick Knutson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • APIs Outside, Events Inside
  • Take Control of Your Application Security
  • SDLC Vs STLC: What's the Difference?
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo