?
Solved

string formatting using sprintf()

Posted on 2004-09-30
2
Medium Priority
?
558 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 46

Accepted Solution

by:
Kent Olsen earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

770 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