Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dissecting a Float Value

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
How can I dissect a value defined as a float?


For example:
float fv=1994;
int int1;
int int2;

I would like to do somthing like:

> sscanf(fv,"2d2d", &int1, &int2);

But this obviously won't work. sscanf is always
expecting it's first argument to be a string. First,
can this even be done, and if so how? Do I need to "Cast"
fv, and if so how?

PS - I like to keep examples simple and to the point!
 
You could do this:
Code:
int1 = (int) fv / 100;
int2 = (int) fv % 100;

Or, if you want to use sscanf, then get fv into a string first:

Code:
char s1[10];
sprintf(s1,"%f",fv);

now you can use s1 in your sscanf statement.
 
I'm pretty sure I tried your 2nd method, i.e.
char s1[10];
sprintf(s1,"%f",fv);

(I'll have to wait until monday to check if I did try this.)

As far as your 1st option, I thought of that too.
But I didn't want to do it that way, because
1.) I thought a "normal" method exist to convert a float
(or int for that matter) to a string, plus
2.) My problem deals with actually a larger float value then my original example.

For example ...
float fv=235955 <--HHMMSS, where int1=23, int2=59, int3=55

and I want to break-up into three values.

I could do this ...

int1 = (int) fv / 10000; /* yields 23 */
int2 = ((int) fv % 10000)/100; /* yields 59 */
int3 = (int) fv % 100; /* yields 55 */

I have a few other even larger permutations of a float to convert as well and just thought there was a quick way to do this (again, provided if method #2 doesn't work).

Thanks for your reply ... maybe when I tried method #2, I probably made a bone-head mistake. Thanks again!!!
 
> float fv=235955 <--HHMMSS, where int1=23, int2=59, int3=55
Well the best thing to do is assign it to an int

int iv = fv;

Then use the / and % chain you have.

But why are you storing them in a float anyway - floats are terribly inaccurate after about 6 decimal digits.
So whilst HHMMSS is probably ok, YYYYMMDDHHMMSS is not



--
 
I was waiting for someone to ask that question (about why storing as a float in the 1st place). The reason is because the original source data is coming in with 1 decimal place (i.e. - 235955.5) which in this example is a .5 second. Thus, I have a choice of either ignoring the .5 second (truncating) or using it to round up or down. A choose the latter.

And yes, I did make a bone-head mistake. I had a &quot;)&quot; misplaced, but it still compiled but was giving me incorrect values. Therefore, what I'm finally going with method-wise is:

float fv=235955.5;
int hh=0;
int mm=0;
int ss=0;
char time[10];

sprintf(time,&quot;06.0f&quot;, fv);
sscanf(time,&quot;%2d%2d%2d&quot;, &hh, &mm, &ss);
printf(&quot;H=%d M=%d S=%d\n&quot;, hh, mm, ss);

produces ...
H=23 M=59 S=56
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top