views:

88

answers:

3

Hi,

i have an integer value

Integer value = 56472201 ; (This could be some time (-17472201) with negetive Integer)

I want this value in this form 56.472201 (i mean if i divide this value/1000000 i get it in general 56.472201) but this does not works in programming as it give the quotient. I want both quotient and remainder in thsi form 56.472201.

Looking for Java solution

thanks.

+2  A: 

cast it to float and then do it:

int i = 56472201;

float j = ((float) i)/1000000.0

Edit: Due to precision(needed in your case), use double. Also as pointed by Konrad Rudolph, no need for explicit casting:

double j = i / 1000000.0;
lalli
Strictly speaking, that cast is redundant when you divide by a `float` anyway (or in your case `double` for almost all languages).
Konrad Rudolph
Good point, I'm just an insecure/paranoid programmer (scared of strtok!)
lalli
thank u guyes It works with both @Konrad Rudolph @lalli and @Guffa Thanks for the solutions
salman
A: 

You have to convert the value to a floating point type first, otherwise you will be doing an integer division.

Example in C#:

int value = 56472201;
double decimalValue = (double)value / 1000000.0;

(The cast is actually not needed in this code, as dividing by a floating point number will cast the value to match, but it's clearer to write out the cast in the code as that is what actually happens.)

Guffa
+1  A: 

If you divide an int by a double you will be left with a double result as illustrated by this unit test.

@Test
public void testIntToDouble() throws Exception {
    final int x = 56472201;
    Assert.assertEquals(56.472201, x / 1e6d);
}

1e6d is 1 * 10^6 represented as a double

Jon Freedman