• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data of more than 7 digits is rounded up by 1  RSS feed

 
Shihad Salam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a small doubt , when using 'Float', data of more than 7 digits is rounded up by 1.
piece of code is given below:
<code>

String _val = "51300123";
Float f = new Float (_val);
System.out.println(f);

</code>

I am getting output as 5.1300124E7 .
But when i tried with 7 digits ie; when put _val = "5130012"; getting exact output as 5130012.0
The problem occurs only when there are more than 7 digits.
Can anyone help me in resolving this. I need to get the exact Output without rounding..
It is Urgent

Thanks,
Shihad
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data types such as float and double have limited precision. The type float has a precision of about 6 or 7 decimal digits. There's no way to store a number in a float that has more than 7 significant digits.

Are your numbers really floating-point values, or are they integers? If they are integers, you could just use int or long instead of float, but these integer types ofcourse also have limits. If you need to store floating-point numbers with unlimited precision, use BigDecimal; if you need to store integer numbers with unlimited precision, use BigInteger. For example:


Shihad Salam wrote:
It is Urgent

Please EaseUp
 
Shihad Salam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,
Thanks for your reply. I have to use this to a general condition, that is, values may be integer or float. It should be applicable to both
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shihad, we don't have too many rules here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites.

http://www.java-forums.org/new-java/43825-data-more-than-7-digits-rounded-up-1-a.html#post206871
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shihad Salam wrote:I have to use this to a general condition, that is, values may be integer or float. It should be applicable to both

Thinking a bit more about this...: Is there actually a reason why you have to store these numbers in memory in a numeric data type? Maybe you can just store the numbers as strings.

If it needs to be a numeric data type, and you have to be able to handle both integers and floating-point numbers, with unlimited precision, then you can use BigDecimal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!