Solved

string formatting using sprintf()

Posted on 2004-09-30
2
548 Views
Last Modified: 2010-04-15

Can someone provide me with the correct formatting for display floating point values..

I know the following:

flat fvar=123;

sprintf(buffer,"%f ",fvar);   // this shows  123.000000
sprintf(buffer,"%.f",fvar);   // this shows  123
sprintf(buffer,"%.2f",fvar);   // this shows  123.00

what is the formatting to force zero padding

i.e

sprintf(buffer,"%05.2f",fvar); // to show  00123.00
sprintf(buffer,"%-5.2f",fvar); // to show  __123.00

thanks


0
Comment
Question by:apakian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 125 total points
ID: 12193411
Hi apakian,

Several things to note about the "%x.yf" format specification.  (x and y are replaced by numbers for actual usage.)

x is the TOTAL width of the output string, not the portion to the left of the decimal point.  If you want to display 5 digits to the left of the decimal and 2 to the right, x will need to be 8.  (5 + 2 + 1 for the decimal point.)

y is the number of digits to the right of the decimal point.

If x starts with a zero, the output string is initialized to all '0' and the converted string placed over these zeros.  The net effect is that you'll see leading zeros.

So to make your last two examples work like your comment, they should look like this:

sprintf(buffer,"%08.2f",fvar); // to show  00123.00
sprintf(buffer,"%-8.2f",fvar); // to show  __123.00



Good Luck,
Kent

0
 

Author Comment

by:apakian
ID: 12193463
brilliant,, thanks heaps..

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question