?
Solved

need quick help asap: printf question

Posted on 2003-03-31
4
Medium Priority
?
220 Views
Last Modified: 2010-04-15
I tried to print out a formatted data.
say, in the program i got these two data:
gDecimal
amount=208.4003

I want to print out the formatted amount according to the gDecimal number i get(I would only know gDecimal at runtime).
how do i print out the number of decimals according to gDecimal number?

say, if gDecimal =2, then amount should be printed out as 208.40
     if gDecimal =3, then amount should be printed out as 208.400
     if gDecimal =4, then amount should be printed out as 208.4003
0
Comment
Question by:pigyc
[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
4 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8239709
since so print the given number of decimal places u have to do something like this

printf("%.2f",float_number); //to print 2 decmal places..

so  to print N decimal places u need to generate the format string dynamically
here is how u can do this

char fmt[128];
sprintf("%%.%df",gDecimal);
gDecimal=  number of decimal places that u need to print..

now u can do
printf(fmt,float_number);

0
 
LVL 5

Accepted Solution

by:
Kocil earned 120 total points
ID: 8239757
use * for width
printf("%.*f",gDecimal, float_number);

0
 
LVL 2

Expert Comment

by:Francoz
ID: 8239853
If the decimal part is a variable then you've to use the * filed to denote it. say for example

printf("%0*.*f",WholeNumber,Decimal,Output);

where WholeNumber will give you the least minimum number of digits used to print the WholeNumber.
Decimal will tell the number of digits to be used to print the decimal after wholenumber.
Output is the real output number to be printed.

///Example:
int WholeNumber = 4, Decimal = 2;
float output = 22.356F;
printf("%0*.*f",WholeNumber,Decimal,output);

The output will be
0022.35

Hope this will clear your doubt.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8242927
ohh * :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

777 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