# setting a float to 4 digit decimal precision

• 12-10-2001
river-wind
setting a float to 4 digit decimal precision
I am dealing with an old language for the front end to a c program. I am running into coredumps when I pass a float that has more than 4 digits after the decimal.

To get around this, I did the following:
passRt=(int)(combRt*10000)

where passRt is an int being passed back to the calling function, and combRt is a float.

Then once the passRt value is back in the calling function, I
LET floatPassRt=passRt/10000
and end up with a 4 digit precision float.

Problem is, this is messing with the rounding.

:confused:
Without having to write an extensive rounding function (tax logic and all), is there a way that I can actually set a varible to be a 4 digit precision float in C?

thanks!!
• 12-10-2001
Sebastiani
Hmm, I have no idea, but what do you mean by a 4 digit precision float? You mean one that does not track the values of lower decimal significance in the float? Sounds interesting anyway...
• 12-10-2001
quzah
sprintf( buf, "%.4d", myFloat );
myFloat = atof( buf );

That might be what you want.

Quzah.
• 12-10-2001
Sebastiani
No, quzah, I think he really means a truncated float.
• 12-10-2001
quzah
Truncated so that it will only accept .0000 decimal points, and nothing beyond that? No, no way to do that with a standard float. You could probably fake it in C++ by making a class...

Quzah.
• 12-10-2001
Sebastiani
Perhaps not tho. Think of it. If you somehow shifted the information aspect of a number to higher orders of value, you might have a float that almost reaches past an unsigned long in largeness but yet keeps track of only 4 orders of decimal precision...iffy but not impossible i think.
• 12-10-2001
quzah
In either case though, you're not dealing with a standard 'float' or 'double'. It'd be something you made to do it. So with standard variables, no, I don't think you can. Floats are stored wierdly in memory. Not the same as standard integers and what not.

Quzah.
• 12-10-2001
Unregistered
Agree with you there.
• 01-21-2002
Unregistered
thanks, guys, I ended up shifting 4 digits, passing as an int, then shifting back. so though its a normal float, it only has four digits that aren't designed to be zero. thanks for the help!