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 8848969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:31:47+00:00 2026-06-14T12:31:47+00:00

Possible Duplicate: Convert decimal to fraction in Objective-C? I’m trying to make an simple

  • 0

Possible Duplicate:
Convert decimal to fraction in Objective-C?

I’m trying to make an simple app for converting decimal form to fraction form. The decimal value is set to a UISlider´s value but how should I get the fraction in fraction form? Should I declare it with double or float and how should I tell the app to print it out in fraction form?

  • 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-06-14T12:31:49+00:00Added an answer on June 14, 2026 at 12:31 pm

    You want to return a fractional approximation from a real number? e.g. 0.3333 ~ “1/3”. You could try experimenting with this:

    #import <Foundation/Foundation.h>
    
    char out[20];
    
    static char *fractApprox(double r, long d) {
        double atof();
        int atoi();
        void exit();
    
        long m[2][2];
        double x, startx;
        long maxden;
        long ai;
    
        startx = x = r;
        maxden = d;
    
        /* 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)x ) + m[1][1] <= maxden) {
            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(x==(double)ai) break;     // AF: division by zero
            x = 1/(x - (double) ai);
            if(x>(double)0x7FFFFFFF) break;  // AF: representation failure
        }
    
        ai = (maxden - 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];
    
        sprintf(out, "%ld/%ld",m[0][0],m[1][0]);
        return out;
    }
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            printf("%s",fractApprox(0.342343, 999));
        }
        return 0;
    }
    

    Prints 329/961 to the console.

    Credits to David Eppstein, UC Irvine for the algorithm in C

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

Sidebar

Related Questions

Possible Duplicate: Convert to absolute value in Objective-C As i am playing with 2d
Possible Duplicate: simple regex in php, formatting decimal number How to convert a digit
Possible Duplicates: How to convert floats to human-readable fractions? Convert decimal to fraction in
Possible Duplicate: C# How to remove 0 from the end of a decimal value
Possible Duplicate: Convert a Unix timestamp to time in Javascript I am trying to
Possible Duplicate: Convert string to float in Objective-C I'd like to convert a string
Possible Duplicate: How to convert decimal to hex in JavaScript? Is there an equivalent
Possible Duplicate: how to convert between degrees, minutes, seconds to Decimal coordinates I have
Possible Duplicate: Why can't decimal numbers be represented exactly in binary? I am trying
Possible Duplicate: convert string to number array in matlab Is there a simple way

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.