Shihad Salam

Greenhorn

Posts: 10

posted 6 years ago

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

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

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

posted 6 years ago

Data types such as

Are your numbers really floating-point values, or are they integers? If they are integers, you could just use

Please EaseUp

`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

posted 6 years ago

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

http://www.java-forums.org/new-java/43825-data-more-than-7-digits-rounded-up-1-a.html#post206871

luck, db
*There are no new questions, but there may be new answers.*

posted 6 years ago

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.

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.