• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data of more than 7 digits is rounded up by 1

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic