Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
please buy my thing and then I'll have more money:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic