Results 1 to 13 of 13
Thread: Doubt in TYPE CASTING
- 07-26-2012, 01:31 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Doubt in TYPE CASTING
Hi All,
Can anyone clarify my doubt in JAVA?
Check my below code
import java.io.*;
public class roundin
{
public static void main(String args[])
{
double amount=138.26;
double tmpAmount =amount*1000;
int tempam=(int)tmpAmount;
System.out.println(tempam);
}
}
output = 138260
When I try by giving value for amount as 118.26 or 138.26 or whatever i get similar outputs.
My doubt is when i try with the value 128.26 alone i am getting the output as 128259. Please any of you clarify me why i am getting so. Please clarify as soon as possible.Last edited by meena22228; 07-26-2012 at 01:42 PM.
- 07-26-2012, 02:04 PM #2
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Doubt in TYPE CASTING
Hi.
I am failing to understand your problem.
but any I hope you understand that by using code below
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.Java Code:int tempam=(int)tmpAmount;
Please advise further if you still not clear.
- 07-26-2012, 02:45 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
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
- 07-26-2012, 02:49 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
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
- 07-26-2012, 02:59 PM #5
Member
- Join Date
- May 2012
- Posts
- 7
- Rep Power
- 0
Re: Doubt in TYPE CASTING
Ok
In your code , just after:
Put an output statement so that you may be able to see the value before it is truncated.Java Code:double tmpAmount =amount*1000;
Like I said in my last post:
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.Java Code:int tempam=(int)tmpAmount;
Only the numbers before (.) are stored in the int.
Hope I am making sense this time.
- 07-26-2012, 03:00 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 07-26-2012, 03:06 PM #7
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Re: Doubt in TYPE CASTING
Considering the above reason rounding might help
int tempam=(int)Math.round(tmpAmount);Swastik
- 07-26-2012, 03:22 PM #8
Re: Doubt in TYPE CASTING
Why do they call it rush hour when nothing moves? - Robin Williams
- 07-26-2012, 03:37 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 07-26-2012, 04:06 PM #10
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
- 07-26-2012, 04:40 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Doubt in TYPE CASTING
Did you read my post?
I explained why in that.Please do not ask for code as refusal often offends.
- 07-26-2012, 08:26 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Doubt in TYPE CASTING
In threads like this one should throw this link in and run away.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-27-2012, 09:35 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks