format fractions (i.e. 0.25 = 1/4) how?

Hello,

Just to save work I was wondering if it's possible to use NSNumberformatter or some other built-in mechanism to format decimal numbers to fractions. (i.e. 0.25 = 1/4 etc.)

On desktop Cocoa.

Thanks.

iMac 24", Mac OS X (10.5.4)

Posted on May 21, 2009 2:52 AM

Reply
4 replies

May 21, 2009 4:59 AM in response to mike.mcfin

I don't think there is a built-in mechanism in Cocoa for the rational numbers (there is one for decimal numbers).

Your problem is a very difficult mathematic problem in the general case. For example, is 0.3333… to be represented as 1/3 or 33333…/100000… ?

If you want to represent only decimal numbers as fraction, it's recommended to use the subclass NSDecimalNumber (you can specify the rouding mode, the rouding scale, …), and you'll have to write a specific Formatter.

If you want to represent any rational number as a fraction, you may create a NSNumber subclass to maange the numerator and denominator.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

format fractions (i.e. 0.25 = 1/4) how?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.