FLOAT to INT??

A00a38ec8eece2121473ffafdb429a31
0
macgadger 101 Feb 01, 2010 at 17:48

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 Replies

Please log in or register to post a reply.

Ad97db76fd8eae1f8f343a80fb99fde8
0
nomad421 101 Feb 01, 2010 at 17:57

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.

A00a38ec8eece2121473ffafdb429a31
0
macgadger 101 Feb 01, 2010 at 18:02

@nomad421

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!!!!