Jump to content


FLOAT to INT??


2 replies to this topic

#1 macgadger

    New Member

  • Members
  • Pip
  • 2 posts

Posted 01 February 2010 - 05:48 PM

Greetings,

I'm looking for conversion routines from a int value to float.

like im converting a float value to integer to read it in 0-100 like

float avg = 0.220000 /* from 0.000000 to 1.000000 */
int ip = (int)(currentVolume*100);

now ip = 20 , that s exactly what i want... !!

now i want to to convert 20 into 0.220000 , opposite int to float

like a function where i can input a integer value betweek 0 to 100 and it converts to 0.000000 to 1.000000


e.g

float SetMeter( int vValue)
{
// conversation code
}



can anyone here help me out of this....


regards,
thnx in advance!!!

#2 nomad421

    Member

  • Members
  • PipPip
  • 46 posts

Posted 01 February 2010 - 05:57 PM

I don't quite understand. The float to int conversion truncates the remainder, so that your current code evaluates as

int ip = (int)(currentVolume * 100);

currentVolume * 100 is first evaluated as a floating point value, 22.00. Thus, your code should result in ip being assigned the value 22, not 20. To convert an integer back to a floating point value, you can simply cast it. To alter the range, you simply divide. Thus, your function should look something like this:

float SetMeter( int vValue)
{
return static_cast<float>(vValue / 100.0f);
}

I would probably same the function more descriptively to ensure that I didn't forget about the division by 100. Note, that given an integer value of 20, this function will return a floating point value of 0.20; not 0.22. Without any extra information about the original value, it is not possible to recover 0.22 from the integer. Hope this helps.

#3 macgadger

    New Member

  • Members
  • Pip
  • 2 posts

Posted 01 February 2010 - 06:02 PM

nomad421 said:

I don't quite understand. The float to int conversion truncates the remainder, so that your current code evaluates as

int ip = (int)(currentVolume * 100);

currentVolume * 100 is first evaluated as a floating point value, 22.00. Thus, your code should result in ip being assigned the value 22, not 20. To convert an integer back to a floating point value, you can simply cast it. To alter the range, you simply divide. Thus, your function should look something like this:

float SetMeter( int vValue)
{
return static_cast<float>(vValue / 100.0f);
}

I would probably same the function more descriptively to ensure that I didn't forget about the division by 100. Note, that given an integer value of 20, this function will return a floating point value of 0.20; not 0.22. Without any extra information about the original value, it is not possible to recover 0.22 from the integer. Hope this helps.


THNX THIS HELPED ME OUT...............!!! :D WORKED LIKE CHARM!!!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users