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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help

Status
Not open for further replies.

rafaelos

Programmer
Mar 17, 2001
2
GR
i am new in c programming.i want to tell me
how can i convert the decimal part of an float number in integer .for instance in the float number 1845.32
i want the integer variable decimal to have the value 32.
 
Here's one way...

#include <stdio.h>
#include <string.h>

main()
{
float f1 = 1845.32;
int r1 = 0;
char *p, sf1[20];

sprintf(sf1, &quot;%6.2f&quot;, f1); /* convert float to string */
printf(&quot;SF1 = %s\n&quot;, sf1);
p = strchr(sf1, '.'); /* find the decimal point */
p++; /* point to the first char after the decimal point */
printf(&quot;P = %s\n&quot;, p);
r1 = atoi(p); /* convert string to int */
printf(&quot;R1 = %d\n&quot;, r1);
return;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top