Thread: Getting more digits after dot

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    GReaper gives the best advice on this, but not the most fun advice I think

    The following is an example of how you can make your own fraction calculations to hundreds of places if you like. If you want to go into the millions or billions, you probably have to ouput it to a file. I think the console window can only hold so much.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
     
    #define SIZE 500
     
    char* calcFraction(int numer, int denom);
     
    int main() {
    	printf("%s\n", calcFraction(78, 7));
    }
     
    char* calcFraction(int numer, int denom) {
    	static char fraction[SIZE];
    	memset(fraction, 0, SIZE);
    	
    	sprintf(fraction, "%d", numer / denom);
    	strcat(fraction, ".");
     
    	for (int i = strlen(fraction); i < SIZE; ++i) {
    		numer = numer % denom * 10;
    		sprintf(&fraction[i], "%d", numer / denom);
    	}
    	fraction[SIZE - 1] = '\0';
     
    	return fraction;
    }
    Last edited by jack jordan; 11-18-2017 at 12:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How many digits?
    By JM1082 in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2011, 03:30 PM
  2. Help on sum digits in C
    By KevinH123 in forum C Programming
    Replies: 7
    Last Post: 12-07-2010, 01:36 AM
  3. how to add 2 digits?
    By nefsan in forum C Programming
    Replies: 16
    Last Post: 03-28-2008, 02:41 PM
  4. get all the right digits
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 11-27-2005, 06:16 AM
  5. Two digits
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2004, 01:53 PM

Tags for this Thread