Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number seperations

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
if I have a floating point number
float = 100.5;
how I can isolate the 'last' digit and store that single number in an int variable.

Eg.
float = 100.5 ;
int = 5;

float = 100.0;
int = 0;

float = 100.7;
int = 7;

Many thanks !!
 
I think this should produce the desired result

an int cast will always just drop the decimal so

int x;
float y;
int z;
int precission = 10;

y = 100.5;
x = y;
z = y/(float)x * precission;

Matt
 
Shouldn't it be

float y;
int x;
int z; /* result */

y = 100.5;
x = y; /* x = 100 */
z = (int)(y * 10.0) % (x * 10); /* Ans: 5 */

(I have not tested it!)
- Karthika
 
You can use 'floor' function.

#include <math.h>
#include <stdio.h>

int main( void )
{
float value = 100.5f;
int i = (int)( ( value - floor( value) ) * 10.f );
printf( &quot;float=%f, int=%d\n&quot;, value, i );

return 0;
}


It is preferable to know the function 'ceil' too.
Hee S. Chung
heesc@netian.com
 
Oh my bad... my z = y/(float)x * precission; from above should read

z = (y - (float)x) * precission;



Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top