Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4241424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:18:17+00:00 2026-05-21T03:18:17+00:00

I am trying to take everything after the decimal and display it as a

  • 0

I am trying to take everything after the decimal and display it as a fraction. Couldn’t find much for objective-c on how to accomplish this. I am using double for my formatting of variables, not sure if that would matter or not. This is how I am formatting for output of my answer:[theTextField setText:[NSString stringWithFormat:@"%f''", (myVariable)]]; This displays ok as decimal, but would really like it as a whole number and fraction (ie.) 7 1/2 instead of 7.5000. Thank you in advanced!

Update:5/13/2011

Well, I got it to display 7 1/16, but something with the math is off. Because it won’t change from 1/16 even by changing the values that are getting divided. Where am I going wrong here? If anyone can actually get this to work correctly please post fully how its done. This is something that should be simple but isn’t and way too time consuming. Please post fully how it is done. Thanks.

Update:
If this answer isn’t working for you check out my other post, this works! Convert decimals to fractions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:18:17+00:00Added an answer on May 21, 2026 at 3:18 am

    Objective-C uses pure C for basically all primitive mathematical operations.

    This being said you’ll find all necessary information (along with C code) in the answers of this other question:

    How to convert floats to human-readable fractions?

    (Specifically this answer featuring actual C code.)

    Here is a quick c function wrapper of said algorithm:

    typedef struct {
        long nominator;
        long denominator;
        double error;
    } Fraction;
    
    /*
     * Find rational approximation to given real number
     * David Eppstein / UC Irvine / 8 Aug 1993
     *
     * With corrections from Arno Formella, May 2008
     * Function wrapper by Regexident, April 2011
     *
     * usage: fractionFromReal(double realNumber, long maxDenominator)
     *   realNumber: is real number to approx
     *   maxDenominator: is the maximum denominator allowed
     *
     * based on the theory of continued fractions
     * if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...)))
     * then best approximation is found by truncating this series
     * (with some adjustments in the last term).
     *
     * Note the fraction can be recovered as the first column of the matrix
     *  ( a1 1 ) ( a2 1 ) ( a3 1 ) ...
     *  ( 1  0 ) ( 1  0 ) ( 1  0 )
     * Instead of keeping the sequence of continued fraction terms,
     * we just keep the last partial product of these matrices.
     */
    Fraction fractionFromReal(double realNumber, long maxDenominator) {
       double atof();
       int atoi();
       void exit();
    
       long m[2][2];
       double startx;
       long ai;
    
       startx = realNumber;
    
       // initialize matrix:
       m[0][0] = m[1][1] = 1;
       m[0][1] = m[1][0] = 0;
    
       // loop finding terms until denom gets too big:
       while (m[1][0] *  (ai = (long)realNumber) + m[1][1] <= maxDenominator) {
           long t;
           t = m[0][0] * ai + m[0][1];
           m[0][1] = m[0][0];
           m[0][0] = t;
           t = m[1][0] * ai + m[1][1];
           m[1][1] = m[1][0];
           m[1][0] = t;
    
           if (realNumber == (double)ai) {
               // AF: division by zero
               break;
           }
    
           realNumber = 1 / (realNumber - (double)ai);
    
           if (realNumber > (double)0x7FFFFFFF) {
               // AF: representation failure
               break;
           }
       }
    
       ai = (maxDenominator - m[1][1]) / m[1][0];
       m[0][0] = m[0][0] * ai + m[0][1];
       m[1][0] = m[1][0] * ai + m[1][1];
       return (Fraction) { .nominator = m[0][0], .denominator = m[1][0], .error = startx - ((double)m[0][0] / (double)m[1][0]) };
    }
    

    Calling it like this:

    double aReal = 123.45;
    long maxDenominator = 42;
    Fraction aFraction = fractionFromReal(aReal, maxDenominator);
    printf("Real %.3f -> fraction => %ld/%ld, error: %.3f\n",
           aReal,
           aFraction.nominator,
           aFraction.denominator,
           aFraction.error);
    

    Prints this:

    Real 123.450 -> fraction => 3827/31, error: -0.002
    

    Last but not least let’s see how we get our newly crafted fraction into out text field:

    double myVariable = 7.5;
    long maxDenominator = 1000; //sample value
    Fraction myFraction = fractionFromReal(abs(myVariable - (NSInteger)myVariable), maxDenominator);
    [theTextField setText:[NSString stringWithFormat:@"%d %d/%d", (NSInteger)myVariable, myFraction.nominator, myFraction.denominator]];
    

    Expected output: "7 1/2", actual output: "7 499/999"
    For some info on why this can happen see this answer to a related question: How to convert floats to human-readable fractions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to take a POCO object and update it with Linq2SQL using an
I am trying to get the Jquery UI modal dialog to take over after
I'm trying to take timestamps in this format: 2009-11-16T14:05:22-08:00 and make turn them into
Trying to have a page which will not take everything from layouts/application.html.erb, Is it
I have a datagridview with 9 columns. I am simply trying take the value
Im trying to take a link either when clicked or hover over be able
I am trying to take a rather large CSV file and insert it into
So I'm trying to take a bilinear interpolation algorithm for resizing images and add
I am trying to take a shapefile of subdivisions within a county that I
I am trying to take the string <BR> in VB.NET and convert it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.