Click to See Complete Forum and Search --> : Another newbie C question


klamath
11-14-2000, 08:35 PM
I'm using printf() to print some numbers (in 'double' format). I want to print as many zeros as are necessary, but no more. If no zeros are necessary, I don't want to print a period after the digit in the ones column. For instance:

5.0000000000 -> 5
5.0005000000 -> 5.0005
5.5000000000 -> 5

(I'm guessing the number of zeros included by default, I can't remember).

How would one do this?

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the BBB (http://bbb.sourceforge.net)

Strike
11-14-2000, 09:55 PM
Hmm... don't know about that for sure. Of course you can always set the precision with a number in the format string. Like %.2f is precision to two decimal places. I don't know if there is a standard C function that will do what you want (though it's probably not beyond your ability to code one).

---edit---
missed the decimal point in the character formatting string

[This message has been edited by Strike (edited 14 November 2000).]

Gweedo
11-14-2000, 10:50 PM
I have to agree with Strike, that there is probably no way to do what your are saying. Setprecision is the closest your going to get.

------------------
Dubbie..Dubbie..Do..
Watch out.. or the Penguin will get You ;)

Strike
11-15-2000, 12:45 AM
You know, now that I look at the man page for printf(3), there's a lot of stuff I didn't know about. If you haven't looked through there, you might find what you want in there.

BrianDrozd
11-15-2000, 09:53 AM
Okay, this is how you do it.
Try:
printf("%.*f", pre, f_num);
where pre is an int precision, and f_num is the float value you want printed. by setting pre to 0, no decimal point will be displayed.

[This message has been edited by BrianDrozd (edited 15 November 2000).]