Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char -> int

Reply
Thread Tools

char -> int

 
 
ern
Guest
Posts: n/a
 
      09-27-2005
Anybody know a quick and dirty function for going from "547.6458679" to
the integer version 548 ?

i.e.

int returnIntegerEquivalent(char * charNum){
int intNum;
//Do some stuff...
return intNum;
}

I was using the Windows stuff before (ie _gcvt() and atof()).
Thanks,

 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      09-27-2005

ern wrote:
> Anybody know a quick and dirty function for going from "547.6458679" to
> the integer version 548 ?
>
> i.e.
>


#include <stdlib.h>
#include <math.h>

> int returnIntegerEquivalent(char * charNum){
> int intNum;


double dNum = strtod(charNum, NULL);
if (ceil(dNum) - dNum > 0.5)
intNum = (int) floor(dNum);
else
intNum = (int) ceil(dNum);

> return intNum;
> }
>
> I was using the Windows stuff before (ie _gcvt() and atof()).
> Thanks,


 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-27-2005
"ern" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Anybody know a quick and dirty function for going from "547.6458679" to
> the integer version 548 ?


rounding to +infinity is done with ceil()
examples:
547.6 -> 548
547.4 -> 548
547 -> 547
-547.4 -> -547
-547.6 -> -547

rounding to -infinity is done with floor()
examples:
547.6 -> 547
547.4 -> 547
547 -> 547
-547.4 -> -548
-547.6 -> -548

truncating the fractional part is done by converting/casting to integer
type:
547.6 -> 547
547.4 -> 547
547 -> 547
-547.4 -> -547
-547.6 -> -547

rounding to the nearest integer can be achieved from truncating if 0.5 is
first [added to]/[subtracted from] the number being rounded:
547.6+0.5 -> 548
547.4+0.5 -> 547
547+0.5 -> 547
-547.4-0.5 -> -547
-547.6-0.5 -> -548

Something like that,
Alex


 
Reply With Quote
 
tedu
Guest
Posts: n/a
 
      09-27-2005
ern wrote:
> Anybody know a quick and dirty function for going from "547.6458679" to
> the integer version 548 ?
>
> i.e.
>
> int returnIntegerEquivalent(char * charNum){
> int intNum;
> //Do some stuff...


intNum = atof(charNum) + 0.5;

> return intNum;
> }
>
> I was using the Windows stuff before (ie _gcvt() and atof()).
> Thanks,


 
Reply With Quote
 
Charles M. Reinke
Guest
Posts: n/a
 
      09-27-2005
"ern" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Anybody know a quick and dirty function for going from "547.6458679" to
> the integer version 548 ?
>
> i.e.
>
> int returnIntegerEquivalent(char * charNum){
> int intNum;
> //Do some stuff...
> return intNum;
> }
>
> I was using the Windows stuff before (ie _gcvt() and atof()).
> Thanks,
>


I'm not sure how "dirty" (or quick, for that matter) it is, but you'll
probably want to look into the strtol() function:
http://ccs.ucsd.edu/c/stdlib.html#strtol. More intuitively, you could
convert the string to a (double) using the related strtod() function
(http://ccs.ucsd.edu/c/stdlib.html#strtod), and then cast the result to an
(int), especially since you have to do that anyway with strtol() unless you
declare "intNUM" of type (long).

-Charles


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-27-2005
ern wrote:
> Anybody know a quick and dirty function for going from "547.6458679" to
> the integer version 548 ?
>
> i.e.
>
> int returnIntegerEquivalent(char * charNum){
> int intNum;
> //Do some stuff...
> return intNum;
> }
>
> I was using the Windows stuff before (ie _gcvt() and atof()).
> Thanks,
>

#include <errno.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

/* making no assumption that s represents an value in the range of an
int, but accepting that some initial portion is supposed to represent
a legal double value. 0 is returned (with errno set) if a problem occurs
with the string to float conversion. */

double floatingstring2floatingint(const char *s)
{
double x, fp, ip;
int sign = 1;
errno = 0;
x = strtod(s, 0);
if (errno)
return 0;
if (x < 0) {
sign = -1;
x = -x;
}
fp = modf(x, &ip);
if (fp >= .5)
ip++;
return sign * ip;
}

int main(void)
{
char s[] = "547.6458679";
char t[] = "-547.6458679";
printf("The initial string (s) is \"%s\n", s);
printf("floatingstring2floatingint(s) returns %g\n",
floatingstring2floatingint(s));
printf("The initial string (t) is \"%s\n", t);
printf("floatingstring2floatingint(t) returns %g\n",
floatingstring2floatingint(t));
return 0;
}



The initial string (s) is "547.6458679
floatingstring2floatingint(s) returns 548
The initial string (t) is "-547.6458679
floatingstring2floatingint(t) returns -548
 
Reply With Quote
 
Charles M. Reinke
Guest
Posts: n/a
 
      09-27-2005

"Charles M. Reinke" <(E-Mail Removed)> wrote in message
news:dhcbhs$cgl$(E-Mail Removed)...
> "ern" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > Anybody know a quick and dirty function for going from "547.6458679" to
> > the integer version 548 ?
> >
> > i.e.
> >
> > int returnIntegerEquivalent(char * charNum){
> > int intNum;
> > //Do some stuff...
> > return intNum;
> > }
> >
> > I was using the Windows stuff before (ie _gcvt() and atof()).
> > Thanks,
> >

>
> I'm not sure how "dirty" (or quick, for that matter) it is, but you'll
> probably want to look into the strtol() function:
> http://ccs.ucsd.edu/c/stdlib.html#strtol. More intuitively, you could
> convert the string to a (double) using the related strtod() function
> (http://ccs.ucsd.edu/c/stdlib.html#strtod), and then cast the result to an
> (int), especially since you have to do that anyway with strtol() unless

you
> declare "intNUM" of type (long).
>
> -Charles
>


After sending my inital response I realized that the proper solution could
be a little complicated, hence the following code:
holomask>cat h.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int returnIntegerEquivalent(char *charNum) {
int intNum;
char *str_test;

intNum = floor(strtod(charNum, &str_test)+0.5);
if(charNum==str_test)
printf("Error converting string.\n");

return intNum;
} /* returnIntegerEquivalent */

int main(void) {
int intNum;
char charNum[]="547.6458679";

intNum = returnIntegerEquivalent(charNum);
printf("test string: %s\ninteger value: %d\n", charNum, intNum);

return 0;
} /* main */
holomask>gcc -Wall -ansi -pedantic -lm -o h.exe h.c
holomask>h.exe
test string: 547.6458679
integer value: 548


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-27-2005
"Charles M. Reinke" <(E-Mail Removed)> wrote in
news:dhch41$esl$(E-Mail Removed):

> intNum = floor(strtod(charNum, &str_test)+0.5);
> if(charNum==str_test)
> printf("Error converting string.\n");


In cases like this, it might be more useful to test that the string
contained nothing other than a valid number. For example, the test above
would not raise an error if

"547.64.58679"

was passed to it.

Thus, a test like

if( *str_test ) {
/* error */
}

might be more useful in practice.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
Charles M. Reinke
Guest
Posts: n/a
 
      09-27-2005

"A. Sinan Unur" <(E-Mail Removed)> wrote in message
news:Xns96DEC24772422asu1cornelledu@127.0.0.1...
> In cases like this, it might be more useful to test that the string
> contained nothing other than a valid number. For example, the test above
> would not raise an error if
>
> "547.64.58679"
>
> was passed to it.


Agreed.

> Thus, a test like
>
> if( *str_test ) {
> /* error */
> }
>
> might be more useful in practice.
>
> Sinan
>


Point well taken.

-Charles


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-27-2005
"Charles M. Reinke" <(E-Mail Removed)> wrote in
news:dhcl7a$gne$(E-Mail Removed):

>
> "A. Sinan Unur" <(E-Mail Removed)> wrote in message
> news:Xns96DEC24772422asu1cornelledu@127.0.0.1...
>> In cases like this, it might be more useful to test that the string
>> contained nothing other than a valid number. For example, the test
>> above would not raise an error if
>>
>> "547.64.58679"
>>
>> was passed to it.

>
> Agreed.
>
>> Thus, a test like
>>
>> if( *str_test ) {
>> /* error */
>> }
>>
>> might be more useful in practice.


....

>
> Point well taken.


I should note that this requires str_test to be initialized.

char *str_test = charNum;

should be good enough.

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments