By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,781 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,781 IT Pros & Developers. It's quick & easy.

Getting decimal part of a float value

P: 9
I would like to get the fractional part i.e., digits after the decimal point in a float value without using any built in functions.

if float x=20.158;
i want to store 158 in other variable.
NOTE: should not use built in functions in java.

Thanks in Advance.
Mar 27 '13 #1
Share this Question
Share on Google+
3 Replies

P: 13,264
What have you tried?
Mar 27 '13 #2

P: 9
I would like to know the logic to separate the numbers next to '.' in a float value.

for example :

float a=20.154;
float b=158.75885;
float c,d;
System.out.println(c); // value in c will be 0.15399999999999991
System.out.println(d); // value in d will be 0.7588499999999954

but want the value of c as just 0.154 or 154 and value of d as 0.75885 or 75885

Thanks in advance
Mar 27 '13 #3

P: 13,264
Fractional values cannot be represented accurately in a binary based computer. You have to start with value as a string to get the a string that represents the exact value.
Mar 27 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.