views:

82

answers:

6

I have a double variable test. I want to extract the all digits before decimal point and two digits after the decimal point store the output in integer variables dollar, cents. how can i do it? I dont want any rounding to happen.

example:

double test= 12.1234

Output

int dollar =12;
int cents =12;

double test =1235.0

output

int dollar=1235
int cents =0
+2  A: 

You can do:

double test= 12.1234;
int dollar = (int) test;
int cents = (int) ((test-dollar) * 100);
codaddict
What about overflows, rounding errors and precission errors? Double is not approriate to store money.
Thomas Jung
@Thomas: Agree. BigDecimal is the solution.
codaddict
Thanks a lot for the info
Arav
What does overflows mean?
Arav
test can have negative values so it should work right?
Arav
A: 

How about something like this. Rather than printing the values, you can simply assign them as you see fit.

double d = 32.456;
System.out.println( (int)d );
System.out.println( d-(int)d);
Mick Walker
+4  A: 

For currency, especially when you don't want any rounding, you should use the BigDecimal class. Something like:

BigDecimal test = new BigDecimal("12.1234");
int dollar = test.intValue();
int cents = test.scaleByPowerOfTen(2).intValue() - dollar * 100;
Konrad Garus
Thanks a lot for the info. Does it work for negative and non decimal values? test = new BigDecimal ("-12.24") test =new BigDecimal("2"); test =new BigDecimal("-2"); Also i retrieve this double value from table through a custom API function. This custom API function retrieves the BigDecimal value from the table and does a doubleValue() returns the double value. I am unable to change this custom API function. In this case if i convert into Bigdecimal and do the calculations will it cause any issues? test=new BigDecimal(BigDecimal.valueof(doubleval));
Arav
It works with integers and negative values as well. Precision loss takes place when the API function converts BigDecimal to double (why does it do it?). I don't think there are any options to deal with it but to work around/extend/override the API.
Konrad Garus
Thanks a lot. Will try your solution and let you know.
Arav
I am getting the output as dollar =12 and cents=1212. I want the cents to be 12. how can i do it?
Arav
See updated answer.
Konrad Garus
A: 
String[] s = Double.toString(test).split(".");
String dol = s[0];
String cent = s[1].substring(0,1);
Moisei
Thanks a lot for the info
Arav
+1  A: 

How about:

dollar = test;
test -= dollar;
cents = test * 100;

Line 1 assigns the integer part of test (12) to the integer 'dollar.' Line 2 removes the 12 dollars from the test value. Line 3 assigns 100 times the fractional part of test to cents. Note that I don't round here. For that, you'd have to:

cents = (test + 0.005) * 100
Simple and good!
Aviator
Line 1 would need a integer cast, else you'll get possible loss of precision error.
codaddict
Thanks a lot for the info. This works for negative values?
Arav
A: 

For double values greater than 10^13, there won't be ANY significant digits after the notional decimal point.

You should not be using double or float for representing financial quantities. Use int or long (appropriately scaled and with explicit checks for overflow) or BigDecimal.

Stephen C
Thanks a lot for the info
Arav
what does overflow mean?
Arav
@arav - Overflow is when a number is too large (positive or negative) to be represented in the data type being used in the calculation. For example, if you are using a Java `int` and add 1 to the largest representable `int` value (`2^31 - 1`), then the computation will *silently* overflow and you will get a negative result; i.e. `-2^31`
Stephen C
Thanks a lot for the info
Arav