Re: Doubt in TYPE CASTING
Hi.
I am failing to understand your problem.
but any I hope you understand that by using code below
Code:
int tempam=(int)tmpAmount;
you are truncating the result you got after multiplying, that meaning in the int only numbers before the commas(.) will be stored not rounded off.
Please advise further if you still not clear.
Re: Doubt in TYPE CASTING
Thanks for ur reply but my doubt is when i try giving value as 138.26 or 118.26 i get output as 138260 or 118260 respectively. I get different output only if i give the value as 128.26 answer for this i get is 128259. I want to know the reason for this
Re: Doubt in TYPE CASTING
To be more clear of my doubt if give 128.26 also i shld get as 128260 only rite but i am not getting as 128260 instead i am getting as 128259. This is my doubt. Hope now you are clear of my doubt
Re: Doubt in TYPE CASTING
Ok
In your code , just after:
Code:
double tmpAmount =amount*1000;
Put an output statement so that you may be able to see the value before it is truncated.
Like I said in my last post:
Code:
int tempam=(int)tmpAmount;
that code does not round-off the number, it just truncates the numbers after (.) ... you are expecting to see a rounded value of 128259.99999999999 which is incorrect.
Only the numbers before (.) are stored in the int.
Hope I am making sense this time.
Re: Doubt in TYPE CASTING
Decimal numbers often do not map to a floating point binary exactly, in the same way that 1/3 does not map exactly as a decimal number.
This is what's happening here.
128.26 is 128.25999999999999 (etc etc) as a floating point.
Re: Doubt in TYPE CASTING
Considering the above reason rounding might help
int tempam=(int)Math.round(tmpAmount);
Re: Doubt in TYPE CASTING
Re: Doubt in TYPE CASTING
Quote:
Originally Posted by
dswastik
Considering the above reason rounding might help
int tempam=(int)Math.round(tmpAmount);
I would suggest not using doubles if they need the accuracy.
To be honest, without knowing exactly what it is they're doing there may well be ways to completely avoid this calculation.
Re: Doubt in TYPE CASTING
Quote:
Originally Posted by
vhonanivb
Ok
In your code , just after:
Code:
double tmpAmount =amount*1000;
Put an output statement so that you may be able to see the value before it is truncated.
Like I said in my last post:
Code:
int tempam=(int)tmpAmount;
that code does not round-off the number, it just truncates the numbers after (.) ... you are expecting to see a rounded value of
128259.99999999999 which is incorrect.
Only the numbers before (.) are stored in the
int.
Hope I am making sense this time.
Thanks for the reply... I did all those already i have an other doubt If we multiply 128.26 by 1000 we must get 128260 only rite while printing i am getting as 12825.9999999.how is it possible.
Re: Doubt in TYPE CASTING
Did you read my post?
I explained why in that.
Re: Doubt in TYPE CASTING
In threads like this one should throw this link in and run away.
kind regards,
Jos
Re: Doubt in TYPE CASTING
I would, but I never have it to hand and can never be bothered to Google it...:)
Besides my "you can't represent 1/3 as a decimal" usually works...clearly not in this case.