We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,602 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Hexadecimal floating constant in Java

I am trying to use such a hexadecimal floating constant "0x0.DAB789B" in java but it gives me java.lang.NumberFormatException. How can I convert hexadecimal float constant to decimal? I know how to convert hexadecimal int constant but that does not work for float constant.

2
Contributors
1
Reply
31 Minutes
Discussion Span
3 Months Ago
Last Updated
11
Views
Question
Answered
hg_fs2002
Light Poster
40 posts since Jul 2010
Reputation Points: 23
Solved Threads: 0
Skill Endorsements: 0

Try the following: Double.parseDouble("0x0.DAB789Bp0").
The p0 means that the part on its left side is multiplied by ( 2 ^ 0 ) = 1.

Sources:

  • Double.valueOf (Java API - referred to from the API documentation of Double.parseDouble)
  • Hexadecimal Floating-Point Literals (Blog - Joseph D. Darcy)
  • Floating-Point Literals (JLS - 3.10.2):

    A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix.
    A floating point number may be written either as a decimal value or as a hexadecimal value.
    For decimal literals, the exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
    For hexadecimal literals, the exponent is always required and is indicated by the ASCII letter p or P followed by an optionally signed integer.

mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
Question Answered as of 3 Months Ago by mvmalderen

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3089 seconds using 2.69MB