Professional Web Applications Themes

Converting from CFString to double - Mac Programming

I'm looking for a way to convert a string entered by the user into a double. I've noticed CFStringGetDoubleValue, but it says that the decimal character must be a "non-localized" decimal character. I'm looking for a way to work the localized decimal character that a user might enter. Is there an supported way to do this? CFNumberFormatter looks like it would do that job, but it requires MacOS 10.3. I need something which can support at least back to MacOS 10.1. -- com http://www.mts.net/~gbeggs1/...

  1. #1

    Default Converting from CFString to double

    I'm looking for a way to convert a string entered by the user into a
    double.

    I've noticed CFStringGetDoubleValue, but it says that the decimal
    character must be a "non-localized" decimal character.

    I'm looking for a way to work the localized decimal character that a
    user might enter.

    Is there an supported way to do this?


    CFNumberFormatter looks like it would do that job, but it requires MacOS
    10.3. I need something which can support at least back to MacOS 10.1.

    --
    com http://www.mts.net/~gbeggs1/
    Gerry Guest

  2. #2

    Default Re: Converting from CFString to double

    In article <1g95kza.oc9maooeypj8N%com>, Gerry
    <com> wrote:
     

    It has been a while since I looked at this, but as I recall there was
    quite a bit of hoop jumping necessary. I don't remember ever playing
    around with CFStringGetDoubleValue().

    I ended up converting the CFString to a Mac-encoded Pascal string, and
    then passing the string to the fp.h conversion stuff. The sequence
    goes something like this:

    CFStringGetPascalString()
    str2dec()
    dec2num()

    After you have a Pascal string, it should be possible to change the
    decimal character as needed.

    The disadvantages of this are that it uses a Pascal string, and the
    need for fp.h, which is infamous for conflicts with math.h, especially
    in CodeWarrior.

    Hope this helps.


    -=-=-=-=-
    Carl Guest

  3. #3

    Default Re: Converting from CFString to double

    On 2004-02-14 15:43:12 -0600, com (Gerry) said:
     

    After just a little digging into the docs here's what I've come up with.

    First, you need to get the CFString into a standard C-string and then
    you can use atof() to get a double from that. The steps seem to be:

    CFString *yourStr;
    double yourVal;
    ...
    char *buf = CFStringGetCStringPtr(yourStr, kCFStringEncodingASCII);
    if (buf != NULL) {
    yourVal = atof(buf);
    }

    Of course, you can try using a different string encoding, but if it's
    getting typed in from the user, then it's probably reasonable to assume
    that it's either this or kCFStringEncodingMacRoman.

    Having not tried this, use at your own risk, but I've used atof() tons
    of times and it's a very reliable, fast method of getting doubles from
    C-strings.
    --

    Thanks,
    Bob (com)
    The Man from S.P.U.D.
    We will write no code before it's designed.

    Bob Guest

  4. #4

    Default Re: Converting from CFString to double

    In article <170220040833266157%com>,
    "Carl R. Osterwald" <com> wrote:
     

    If you are willing to jump outside of the world of CF and call existing
    Carbon routines, there already exist within the system formatting
    routines that can be used to convert strings to doubles (and back) which
    respect the user's selected formatting characters. These routines
    handle much more than simply replacing the decimal character.

    Here are some routines that I use for doing this convertion, with
    application controlled limits on the number of decimal places that are
    output.

    These routines should be compatible with System 7 and up, work on
    Classic, 68k, PPC, Carbon, OS X, and Windows (where I'm lame and just
    call sprintf, etc).

    //
    -------------------------------------------------------------------------
    -------------------------
    void CreateCannonicalFormat(short numPlaces, NumFormatString
    *cannonicalFormat, NumberParts *parts)
    /*
    Convert our desired format into a description of that format. Use
    to prep for the extended<->string
    conversion routines.
    */
    {
    #if TARGET_OS_MAC
    Str255 formatString;
    short i;

    // get the correct current formatting information
    {
    Handle intlTable=0;
    long numPartsOffset;
    long numPartsLength;

    GetIntlResourceTable(GetScriptManagerVariable(smSy sScript),
    smNumberPartsTable, &intlTable, &numPartsOffset, &numPartsLength);
    if (intlTable)
    {
    *parts = *((NumberParts*) (*intlTable + numPartsOffset));

    // Systems 8.5 and under, Swiss-German, had illegal number
    parts tables which had
    // the thousands sep the same as the quote. Let's fix that.
    if (parts->data[tokThousands-1].a[1] ==
    parts->data[tokLeftQuote-1].a[1])
    parts->data[tokLeftQuote-1].a[1] = ' ';
    if (parts->data[tokThousands-1].a[1] ==
    parts->data[tokRightQuote-1].a[1])
    parts->data[tokRightQuote-1].a[1] = ' ';
    }
    }

    // we want numPlaces after the decimal, and 7 before (maximum value
    for a Fixed point #)
    // total length of the formatting string cannot be > 20 characters
    if (numPlaces == kNoPrecision)
    numPlaces = 25-7;
    formatString[0] = numPlaces + 7 + 1;
    for (i = 1; i <= formatString[0]; ++i)
    formatString[i] = parts->data[tokNonLeader-1].a[1];
    if ((numPlaces > 0) || (numPlaces == kNoPrecision))
    {
    formatString[7] = parts->data[tokZeroLead-1].a[1];
    formatString[8] = parts->data[tokDecPoint-1].a[1];
    }
    if (numPlaces == 0)
    formatString[0] = 7;
    if (formatString[0] > 15)
    formatString[0] = 15;
    StringToFormatRec(formatString, parts, cannonicalFormat);
    #endif
    } // CreateCannonicalFormat

    //
    -------------------------------------------------------------------------
    -------------------------
    void ConvertDoubleToString(double theValue, Str255 theString, short
    numPlaces)
    {
    extended80 xValue;
    NumFormatString cannonicalFormat;
    NumberParts parts;

    // grab our format, and copy of the parts table
    CreateCannonicalFormat(numPlaces, &cannonicalFormat, &parts);

    // finally, use that format to convert our number info a string
    #if TARGET_OS_MAC
    {
    #if TARGET_CPU_68K
    xValue = (extended80) theValue;
    #else
    dtox80(&theValue, &xValue);
    #endif
    ExtendedToString(&xValue, &cannonicalFormat, &parts, theString);
    }
    #else
    {
    char formatString[] = "%2.0lf";

    if ( (numPlaces >= 1) && (numPlaces <= 9) )
    formatString[3] = '0' + numPlaces;
    sprintf(theString,formatString,theValue);
    c2pstr(theString);
    }
    #endif

    } // ConvertDoubleToString

    //
    -------------------------------------------------------------------------
    -------------------------
    double ConvertStringToDouble(Str255 theString, short numPlaces)
    {
    extended80 xValue;
    NumFormatString cannonicalFormat;
    NumberParts parts;

    // grab our format, and copy of the parts table
    CreateCannonicalFormat(numPlaces, &cannonicalFormat, &parts);

    // finally, use that format to convert our number info a string
    #if TARGET_OS_MAC
    {
    double dValue;
    StringToExtended(theString, &cannonicalFormat, &parts, &xValue);

    #if TARGET_CPU_68K
    dValue = (double)xValue;
    #else
    dValue = x80tod(&xValue);
    #endif
    return(dValue);
    }
    #else
    {
    double dValue;

    p2cstr(theString);
    sscanf(theString,"%lf",&dValue);
    c2pstr(theString);
    return(dValue);
    }
    #endif
    } // ConvertStringToDouble
    Tom Guest

Similar Threads

  1. Converting into PDF bug...
    By daniel_bittleston@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 2
    Last Post: July 7th, 03:13 AM
  2. converting varchar to double
    By Frederik Vanderhaeghe in forum ASP.NET Building Controls
    Replies: 3
    Last Post: April 21st, 08:29 AM
  3. Converting wav to wma
    By gabriel in forum FreeBSD
    Replies: 2
    Last Post: February 24th, 09:54 PM
  4. CFString interrupt safe?
    By Carl in forum Mac Programming
    Replies: 3
    Last Post: February 3rd, 03:08 PM
  5. Converting C++2C?
    By Thomas in forum UNIX Programming
    Replies: 4
    Last Post: August 12th, 02:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139