Convert char to float
Dave Eisen
dkeisen at Gang-of-Four.Stanford.EDU
Sat Dec 22 05:08:26 AEST 1990
In article <5051 at gara.une.oz.au> swijaya at gara.une.oz.au (Sastra Wijaya STMA) writes:
>I need to know how to convert a char variable into a float. The problem
No you don't. You want to convert a time as stored in a struct tm into a
float. Let me say that (unless you are using a preexisting library
that expects dates in a floating point format) you probably shouldn't do
this. Floating point numbers are for things that really do have
floating decimal points (READ: Scientific computation) and should not
be used for things like money and dates that can be stored in a
format more suited to their structure. I personally find a struct
tm to be a very convenient way of handling dates, we sometimes also use
a character string "YYMMDD.HHSS" or two ints -- yymmdd and hhss. I
can't see what a float buys you.
>the variable tm is a struct of char.
No. It is a struct tm. It has nothing to do with characters.
>
>
>elapsed = (tm2.hour-tm.hour)*3600+(tm2.minute-tm.minute)*60+(tm2.second-tm.second) + (tm2.hsecond-tm.hsecond)/100;
You are using elapsed time in hundredths of seconds, this strikes as being
the same as working with money.
The best way to do this is to adopt a convention that time is measured
in hundredths of seconds. Then do
int elapsed;
elapsed = (tm2.hour - tm.hour) * 3600 * 100 + (tm2.minute - .....
If you ***really*** want to use floating point, your method seems as
good as any.
--
Dave Eisen dkeisen at Gang-of-Four.Stanford.EDU
1447 N. Shoreline Blvd.
Mountain View, CA 94043
(415) 967-5644
More information about the Comp.lang.c
mailing list