Rajesh Kandhasamy

Greenhorn

Posts: 15

posted 10 years ago

Hi,

I am getting the double value like below

123456789123456789123456789123456789.22446688

I need to find the length of this double value.

I have tried the following ways:

1) Pretty ordinary way, just converted the incoming value to String and find the length- It is giving wrong length.

2) Used NumerberFormat class like below:

double value=123456789123456123564789121.12345;

DecimalFormat format=new DecimalFormat(".");

String s =format.format(value);

s.length� will give us the length of the double including DOT except length of the digits after the decimal point.

3) If am replacing the line DecimalFormat format=new DecimalFormat ("#"); then,

s.length� will give us the length of the double excluding DOT except length of the digits after the decimal point.

4) We can able to get the length of the double value before decimal point with option 2 and 3. But, we are not able to find the length of the digits after decimal point. I tried with substring, it is also got failed.

5) Then, I have tried using the custom NumberFormat instance, it is giving the length of the double with minor deviation, we can understands that this minor difference due to custom NumberFormatting, I am not sure which format will give us the correct length of the double.

Also used BigDecimal,Please let us if anyone know the solution for this

Thanks in advance

I am getting the double value like below

123456789123456789123456789123456789.22446688

I need to find the length of this double value.

I have tried the following ways:

1) Pretty ordinary way, just converted the incoming value to String and find the length- It is giving wrong length.

2) Used NumerberFormat class like below:

double value=123456789123456123564789121.12345;

DecimalFormat format=new DecimalFormat(".");

String s =format.format(value);

s.length� will give us the length of the double including DOT except length of the digits after the decimal point.

3) If am replacing the line DecimalFormat format=new DecimalFormat ("#"); then,

s.length� will give us the length of the double excluding DOT except length of the digits after the decimal point.

4) We can able to get the length of the double value before decimal point with option 2 and 3. But, we are not able to find the length of the digits after decimal point. I tried with substring, it is also got failed.

5) Then, I have tried using the custom NumberFormat instance, it is giving the length of the double with minor deviation, we can understands that this minor difference due to custom NumberFormatting, I am not sure which format will give us the correct length of the double.

Also used BigDecimal,Please let us if anyone know the solution for this

Thanks in advance

posted 10 years ago

Firstly you aren't telling us how 'you are getting the value', secondly in each case you don't describe the expected value or how you know each test is incorrect.

If you are receiving the value as a double and then trying to work out how many digits there are in the base 10 representation, you're going to be in trouble as the number will be storred in memory in binary and you can't represent a base 10 decimal exactly in base 2.

Can you drop code snippets for each of the test you performed so we can better understand?

If you are receiving the value as a double and then trying to work out how many digits there are in the base 10 representation, you're going to be in trouble as the number will be storred in memory in binary and you can't represent a base 10 decimal exactly in base 2.

Can you drop code snippets for each of the test you performed so we can better understand?