Solved

string formatting using sprintf()

Posted on 2004-09-30
2
546 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Converting a float to a string function in c languange 10 210
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 68
Detect CR LF to each line 12 174
Reactjs with .NET 3 74
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

756 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