Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i convert Integer value to decimal value?

i have an Integer value:

Integer value = 56472201;

Where the value could be positive or negative.

When I divide the value by 1000000, I want this result in the form 56.472201 but instead it gives me just the quotient. How am I able to get both the quotient and remainder values?

like image 595
jimmy Avatar asked Sep 14 '10 09:09

jimmy


2 Answers

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;
like image 60
lalli Avatar answered Sep 21 '22 14:09

lalli


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

like image 25
Jon Freedman Avatar answered Sep 21 '22 14:09

Jon Freedman