Skip to content

Instantly share code, notes, and snippets.

@amanuel2
Created November 7, 2016 16:19
Show Gist options
  • Select an option

  • Save amanuel2/e8a9080ec92206c77d44886ba14abb29 to your computer and use it in GitHub Desktop.

Select an option

Save amanuel2/e8a9080ec92206c77d44886ba14abb29 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int powk(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return powk(x, y/2)*powk(x, y/2);
else
return x*powk(x, y/2)*powk(x, y/2);
}
void ftoa(float flt, int after_point)
{
int int_flt = (int) flt;
printf("%d" , int_flt);
printf(".");
int mut_ab = powk(10,after_point);
int after_point_flt = (int) (flt * mut_ab); //33314
int multiplier = powk(10,(after_point-1));
//printf("MULTIPLIER : %d\n" , multiplier);
for(int xy=0; xy<after_point; xy++)
{
int muted = (after_point_flt/multiplier);
//printf("%d\n",muted);
muted = muted % 10;
printf("%d", muted);
multiplier/=10;
}
//printf("%d", after_point_flt);
}
int main(void) {
float pi = 333.14434;
ftoa(pi,5);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment