posted 6 years ago

Hi

I have a scenario where i need to know the number of digits after decimal point of a double value.

Lets say

i have

Now i would want to write a code to know how many digits the double value has after decimal point(i.e .)

which in this case is 4.

I tried with

but this gives me result as 0.0 and hence the

comes as 1.

Please help me.

Thanks in advance

Prashant

I have a scenario where i need to know the number of digits after decimal point of a double value.

Lets say

i have

Now i would want to write a code to know how many digits the double value has after decimal point(i.e .)

which in this case is 4.

I tried with

but this gives me result as 0.0 and hence the

comes as 1.

Please help me.

Thanks in advance

Prashant

posted 6 years ago

This is not possible in the way you think.

A

That is really strange, because "0.0" is 3 characters, not 1.

If you want to format a number with a specified number of decimals after the decimal point, you have to specify that at the place you're converting the number to a string. The number of decimals is not some property of the number itself.

A

`double`is just a number. It doesn't have a specified number of digits after the decimal point. 0.000 is exactly the same as 0.0 or 0.00000000 or 0. It's just zero.Prashant Langade wrote:I tried with

but this gives me result as 0.0 and hence the

comes as 1.

That is really strange, because "0.0" is 3 characters, not 1.

If you want to format a number with a specified number of decimals after the decimal point, you have to specify that at the place you're converting the number to a string. The number of decimals is not some property of the number itself.

posted 6 years ago

What you want can't be done with

What you can do is use a BigDecimal with its String constructor, viz:

because that

Winston

Prashant Langade wrote:Please help me.

What you want can't be done with

`Double/double`or

`Float/float`, because the literal 0.000 is identical to 0.0 (and so is its binary form).

What you can do is use a BigDecimal with its String constructor, viz:

`BigDecimal bd = new BigDecimal("0.000");`

because that

*will*retain the number of places after the decimal point.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Prashant Langade

Ranch Hand

Posts: 73

posted 6 years ago

No, you don't. You have a

If you do this:

then

Prashant Langade wrote:However my problem is that i have that 0.000 value in double and not in String

No, you don't. You have a

`double`variable that has the value zero. Zero is zero and is exactly the same as 0, 0.0, 0.000, 0.000000000 or however many zeroes you bother to write.

If you do this:

then

`a`and

`b`have exactly the same value. It is impossible to know with how many decimal digits you wrote it in your source code, because there is no difference. The number of zeroes you wrote after the decimal point is not a property of the number that you can determine later on.

posted 6 years ago

What Jesper said.

However, at some point

Winston

Prashant Langade wrote:However my problem is that i have that 0.000 value in double and not in String

What Jesper said.

However, at some point

*you*must have decided that you wanted 3 decimal places in the number; and the only way to store that value

*and*retain that information is to use BigDecimal.

Winston

Articles by Winston can be found here