0

I want to separate float value from decimal point.

for ex:
Float f = 3.5

than i want

int value1 = 3
int value2 = 5
and if f = 4.0 than,
int value1 = 4
int value2 = 0

But please i could not do this with string manipulations. like Split

Thanks

asked Apr 28, 2011 at 17:12
1
  • 1
    What do you want value2 to be if there's more than one decimal place, e.g 3.54321? Commented Apr 28, 2011 at 17:20

2 Answers 2

1

simply you can do is

int integer_part=(int)f;
float float_part=(f-integer_part);
string string_f=f+"";
int float_part=f*(string_f.length()-1)*10
answered Apr 28, 2011 at 17:27
Sign up to request clarification or add additional context in comments.

4 Comments

you have 2 variables of different types with the same name. Other than that, its an interesting solution
This will have the same problem as John's solution, i.e. if f > Integer.MAX_VALUE, you cannot cast it to a useful int.
you can use long, by the way do you have any problem in using string
if you can be specific to your problem then we could provide some solution. so plz edit and provide more details about your problem
0

If you simply want the ones digit in value 1 and the tenths digit in value 2, you can do this:

value1 = (int)f;
value2 = ((int)(f * 10)) % 10;
answered Apr 28, 2011 at 17:24

3 Comments

@Matt, true, it would overflow. Then he could do f = f - value1; first for value2, that would work.
The first line would still not work, i.e. casting a float > Integer.MAX_VALUE to an integer would never work. So value1 would never be accurate.
In that case, there would be no solution then, except to cast it to a long. I think that might be over-analyzing this issue a bit.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.