[comp.sys.mac.programmer] Floating Point #'s as Strings in Dialogs using Think C 3.0

isler@grad1.cis.upenn.edu (06/08/90)

I am writing a program in Think C 3.0 in which I would like to convert
floating point numbers to strings and place them in EditText items in
a dialog box. 

my code looks like this:

{
 .
 .
 .
 .
 Str255     str;
 float 	    Value;
 Handle     myHandle;
 Handle     myBox;
 DialogPtr  theDialog;

 theDialog = GetNewDialog(IdNumber, 0, - 1);

 GetDItem(theDialog, itemnum, &myType, &myHandle, &myBox);
 sprintf(str, "%3.1f", value);
 SetIText(myHandle, str);

 .
 .
 .
}

The result is that garbage is printed in the dialog instead of the
converted string. The above conversion method works fine when I am
displaying floats as strings in windows, but it does not work here.

Any suggestions?


S-K ISLER

isler@grad1.cis.upenn.edu

anderson@Apple.COM (Clark Anderson) (06/10/90)

 
>From: isler@grad1.cis.upenn.edu:
>The result is that garbage is printed in the dialog instead of the
>converted string. The above conversion method works fine when I am
>displaying floats as strings in windows, but it does not work here.

I'm not a C expert (yet), but since you're making a Pascal Toolbox
call with SetIText, don't you need to cast your numeric string
to be a Pacal type? (With /p, or something like that)
                                        
                                              --clark

-- 
-----------------------------------------------------------
Clark Anderson                  InterNet:  anderson@apple.com
CPU Engineering                 AppleLink: C.ANDERSON
Apple Computer, Inc             BellNet:   408-974-4593

"I speak only for myself, much to my employer's relief..."
-------------------------------------------------------------

jackiw@cs.swarthmore.edu (Nick Jackiw) (06/12/90)

isler@grad1.cis.upenn.edu () writes:
> I am writing a program in Think C 3.0 in which I would like to convert
> floating point numbers to strings and place them in EditText items in
> a dialog box. 
>  sprintf(str, "%3.1f", value);
> 
> Any suggestions?
> S-K ISLER

As mentioned previously, you'll need a Pascal string instead of a c-string.
You may be better served by using the Toolbox, instead of C, to do your
conversions for you.  SANE implements the calls:

procedure Str2Dec(s:DecStr;var Index:integer; var d:Decimal;
                  var validPrefix:boolean);

procedure Dec2Str(f:Decform; d:Decimal; var s:DecStr);

procedure CStr2Dec(s:CStrPtr;var index:integer; var d:Decimal;
                  var validPrefix:boolean);

which will do all the necessary conversions for you.  In addition to
providing the appropriate types to your task, SANE is script-manager
compatible, which may or may not be true of THINK C's sprintf. This
means that if your application is for public use, it will format (and
accept formatted numbers) appropriately to the country in which it's
in use.

See your SANE interface files for declarations of the individual types.



--
-----Nicholas Jackiw [jackiw@cs.swarthmore.edu|jackiw@swarthmr.bitnet]-----
"... Then, with an infernal shovel that increases my strength, I dig out of
that inexhaustable mine whole chunks of lice, big as mountains.  I split them
up with an axe and I transport them in the depths of night to city streets."

stoms@castor.ncgia.ucsb.edu (David Stoms) (06/12/90)

In article <25837@netnews.upenn.edu> isler@grad1.cis.upenn.edu () writes:
>I am writing a program in Think C 3.0 in which I would like to convert
>floating point numbers to strings and place them in EditText items in
>a dialog box. 

>The result is that garbage is printed in the dialog instead of the
>converted string. The above conversion method works fine when I am
>displaying floats as strings in windows, but it does not work here.

typedef char[32] FStr;

Double2String(double num, int digits, FStr string)
	{
	char	bcdNum[12];
	int		i, j;
	char	c;
	
	if (!string) Debugger();
	
/* I believe SANE does this too */
	asm {
		fmove.x	num,fp0
		fmove.p	fp0,bcdNum{#17}
		}
	
	j = 1;
	c = bcdNum[3];
	if (bcdNum[0] & 0x8000) string[j++] = '-';
	string[j++] = (c & 0xf) + '0';
	string[j++] = '.';
	
	digits = 16 - digits;
	if (nbtwn(digits, 0, 16)) Debugger();
	
	for (i=4; i <= 11 - (digits>>1); i++) {
		c = bcdNum[i];
		string[j++] = (c>>4 & 0xf) + '0';
		string[j++] = (c & 0xf) + '0';
		}
	j -= digits % 2;
	/* while (string[j-1] == '0') j--; */
	if (string[j-1] == '.') j--;
	
	string[j++] = 'e';
	string[j++] = (bcdNum[0] & 0x4000) ? '-' : '+';
	
	i = false;
	if ( (bcdNum[0] & 0xf) != 0 ) {
		string[j++] = (bcdNum[0] & 0xf) + '0';
		i = true;		/* significant digit */
		}
	if ( i || (bcdNum[1]>>4 & 0xf) != 0) {
		string[j++] = (bcdNum[1]>>4 & 0xf) + '0';
		i = true;
		}
	if ( !i && bcdNum[1] & 0xf == 0)
		j -= 4;
	else
		string[j++] = (bcdNum[1] & 0xf) + '0';
	
	string[0] = j-1;
	}

Josh.