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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing floating point precision with printf

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
I want to know how to show the correct amount of precision with printf. For example, if I say printf("%f", fVal) and fVal is 1, it shows 1.00000, and I want it to show 1 or 1.0. If fVal is 1.23, I want it to show 1.23, not 1.23000 or 1.2. In other words, I want to show the amount of precision that the value passed actually has, which means I can't just send "%f" or "%.2f" to printf. Is there a way to do what I'm trying to do?
 
Try %W.Pg specification (P == all digits with g spec). I think, it's the best choice in your case.

But remember: 0.1 is NOT equal to 1/10 on binary arithmetic processors! It's a (good) approximation to 1/10. Don't hunt for ghosts...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top