Computing.Net > Forums > Programming > decimal formatting

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

decimal formatting

Reply to Message Icon

Name: saddam (by lurker)
Date: April 19, 2005 at 22:11:50 Pacific
Comment:

hello again,

Is it possible for the user to specify how many decimal places to format a number in C? I could use switch but the user should be able to specify and number. For example:

fprintf(STDOUT, "%4d", num);

prints a number to 4 decimal places. Or how do I get the fprintf function to print the number of places in decplaces?

int decplaces;
.....
.....
fprintf(STDOUT, "%d", num);

saddam



Sponsored Link
Ads by Google

Response Number 1
Name: sen (by santanusen_82)
Date: April 19, 2005 at 22:53:31 Pacific
Reply:

Mr. Ex-President,

I'll only answer your last question and that'll make everything clear. Use -


fprintf(stdout, "%*d", decplaces, num);


Hope you've got what you want. Best of luck.

Santanu Sen
National Institute of Technology
Durgapur
India


0

Response Number 2
Name: saddam (by lurker)
Date: April 20, 2005 at 04:01:37 Pacific
Reply:

hello,

Many thanks, you are the best!! I never thought both my questions would be answered by the same person! As of the pervious one no one seem to answer it and it was beginning to slip away.

I am sorry but could you explain what the * is all about and what it does?

thanks again.

saddam


0

Response Number 3
Name: sen (by santanusen_82)
Date: April 20, 2005 at 04:34:12 Pacific
Reply:

Hi,
It's really great to have such complements from an ex-president. Anyways, let me explain what you want to know.

When you write some code segment like


int a = 10;
fprintf(stdout, "%15d", a);

it prints the value of 'a' using 15 places. That is it will print 13 blanks then '10'. You already know that.


Now if you write


int a = 10;
int b = 15;

fprintf(stdout, "%*d", b, a);

It will generate the same output as the previous one. You may visualize it this way, first it replaces the * sign of the %*d flag with the value of 'b'. That is, it becomes %15d. Now it will print the value of a using this precision. Similarly, were the value of 'b' 8 it would have printed the value of 'a' using %8d. Note that the value of 'b' is not printed. It is only used to set the precision.

Hope I am clear enough. Best of luck.

Santanu Sen
National Institute of Technology
Durgapur
India


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


how to use smbclient in j... Java Copy and Paste Probl...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: decimal formatting

C : Reading from a file www.computing.net/answers/programming/c-reading-from-a-file/4682.html

IIS Regional Setting DLL Problem www.computing.net/answers/programming/iis-regional-setting-dll-problem/2273.html

07 hex www.computing.net/answers/programming/07-hex/16100.html