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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:59:05+00:00 2026-05-15T05:59:05+00:00

Is there a simple way to do something like.. [NSMagicDataConverter humanStringWithBytes:20000000] ..which would return

  • 0

Is there a simple way to do something like..

[NSMagicDataConverter humanStringWithBytes:20000000]

..which would return “19.1MB”?

  • 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-15T05:59:06+00:00Added an answer on May 15, 2026 at 5:59 am

    Here’s my own take on the problem:

    enum {
        kUnitStringBinaryUnits     = 1 << 0,
        kUnitStringOSNativeUnits   = 1 << 1,
        kUnitStringLocalizedFormat = 1 << 2
    };
    
    NSString* unitStringFromBytes(double bytes, uint8_t flags){
    
        static const char units[] = { '\0', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
        static int maxUnits = sizeof units - 1;
    
        int multiplier = (flags & kUnitStringOSNativeUnits && !leopardOrGreater() || flags & kUnitStringBinaryUnits) ? 1024 : 1000;
        int exponent = 0;
    
        while (bytes >= multiplier && exponent < maxUnits) {
            bytes /= multiplier;
            exponent++;
        }
        NSNumberFormatter* formatter = [[[NSNumberFormatter alloc] init] autorelease];
        [formatter setMaximumFractionDigits:2];
        if (flags & kUnitStringLocalizedFormat) {
            [formatter setNumberStyle: NSNumberFormatterDecimalStyle];
        }
        // Beware of reusing this format string. -[NSString stringWithFormat] ignores \0, *printf does not.
        return [NSString stringWithFormat:@"%@ %cB", [formatter stringFromNumber: [NSNumber numberWithDouble: bytes]], units[exponent]];
    }
    

    By default (if 0 is passed for flags), it will output SI units (base ten). You can set kUnitStringBinaryUnits to select binary (base two) units suitable for memory, or kUnitStringOSNativeUnits to have the unit type selected automatically based on OS version (pre-Leopard gets base two, post-Leopard gets base ten). Setting kUnitStringLocalizedFormat formats the string based on the user’s current locale. For example:

    unitStringFromBytes(1073741824, 0); // → "1.07 GB"
    unitStringFromBytes(1073741824, kUnitStringBinaryUnits); // → "1 GB"
    unitStringFromBytes(1073741824, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "1.07 GB" (In Mac OS 10.6)
    unitStringFromBytes(12345678901234567890123456789, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "12,345.68 YB" (In Mac OS 10.6, in the US)
    unitStringFromBytes(12345678901234567890123456789, kUnitStringOSNativeUnits | kUnitStringLocalizedFormat); // → "12.345,68 YB" (In Mac OS 10.6, in Spain)
    

    Here’s the helper function required for OS-native units:

    BOOL leopardOrGreater(){
        static BOOL alreadyComputedOS = NO;
        static BOOL leopardOrGreater = NO;
        if (!alreadyComputedOS) {
            SInt32 majorVersion, minorVersion;
            Gestalt(gestaltSystemVersionMajor, &majorVersion);
            Gestalt(gestaltSystemVersionMinor, &minorVersion);
            leopardOrGreater = ((majorVersion == 10 && minorVersion >= 5) || majorVersion > 10);
            alreadyComputedOS = YES;
        }
        return leopardOrGreater;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple way to create a BindingList wrapper (with projection), which would
Is there a simple way to make a game loop in JavaScript? something like...
Is there a simple way to add something like the {Back to Worg's index}
Is there a simple way to get driving instructions? Something like: I call http://someservice/?start=NYC&target=Washington
Is there a simple way to encrypt/decrypt a string with a key? Something like:
Is there a simple way in Ruby to get a true/false value from something
Is there a simple way to see assigned shortcuts with given prefix? Something similar
Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon
Is there a simple way to move an element inside its own parent? Like
Is there a simple way of animating rows in an NSTableView? I'd like 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.