Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

need quick help asap: printf question

Posted on 2003-03-31
4
Medium Priority
?
227 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
  • 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

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…
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.
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

564 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