0

In web application, i write the code like this :

  float f= 659/1024 

but i am getting the resule in f is 0.0 where it has to be 0.6458 smething

3 Answers 3

2

That is because both values are integers. Use 659.0/1024 or 659F/1024 instead.

Sign up to request clarification or add additional context in comments.

Comments

2

You are dividing two integers, so the result will be 0. You have to cast the numbers to float:

float f = (float)659/(float)1024

1 Comment

One of the numbers will be enough.
2

You are seeing this because you are performing integer division and then assigning the result to a float. Try the following instead:

float f = 659.0 / 1024;

Or to be more explicit:

float f = (float)659 / 1024;

Note that only one of the numbers needs to be a float to make the operation perform floating point arithmetic instead of integer arithmetic.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.