remove decimal but show cents with sprintf

C

Chad

I would like to remove the decimal but still show the cents using
sprintf.


char empheader[72];
double totalwage = 123.45;


sprintf(empheader, "%.2f", totalwage);


This formats totalwage to show 123.45, I want it to show 12345. I'm
creating an electronic file for wage reporting and the decimal value is
assumed so the dot shouldn't be displayed. Can this be done with
sprintf?
 
A

Ancient_Hacker

Chad said:
I would like to remove the decimal but still show the cents using
sprintf.


char empheader[72];
double totalwage = 123.45;


sprintf(empheader, "%.2f", totalwage);


This formats totalwage to show 123.45, I want it to show 12345. I'm
creating an electronic file for wage reporting and the decimal value is
assumed so the dot shouldn't be displayed. Can this be done with
sprintf?

Easiest way I can think of:

sprintf( empheader, "%d", (int) (totalwage * 100.0 ) );
 
T

Tom St Denis

Chad said:
I would like to remove the decimal but still show the cents using
sprintf.


char empheader[72];
double totalwage = 123.45;


sprintf(empheader, "%.2f", totalwage);

If you must...

snprintf(empheader, sizeof(empheader), "%d", (int)((totalwage -
floor(totalwage)) * 100));

Tom
 
E

Eric Sosman

Ancient_Hacker wrote On 08/22/06 10:34,:
Chad said:
I would like to remove the decimal but still show the cents using
sprintf.


char empheader[72];
double totalwage = 123.45;


sprintf(empheader, "%.2f", totalwage);


This formats totalwage to show 123.45, I want it to show 12345. I'm
creating an electronic file for wage reporting and the decimal value is
assumed so the dot shouldn't be displayed. Can this be done with
sprintf?


Easiest way I can think of:

sprintf( empheader, "%d", (int) (totalwage * 100.0 ) );

Even easier than "easiest:"

sprintf (empheader, "%.0f", totalwage * 100.0);

.... with the added advantage of correct rounding.
 
B

Ben Pfaff

Eric Sosman said:
Ancient_Hacker wrote On 08/22/06 10:34,:

Even easier than "easiest:"

sprintf (empheader, "%.0f", totalwage * 100.0);

... with the added advantage of correct rounding.

Having read and re-read the classic paper on printing
floating-point numbers several times in the last few weeks, I'd
like to point out that should really be an advantage of *a better
chance* of correct rounding rather than an absolute statement.
Just multiplying by 100 can wreck things. (Of course, if I
misinterpreted the paper, and someone corrects me on it, then
I'll learn something from that, too.)
 
M

Michal Nazarewicz

Chad said:
char empheader[72];
double totalwage = 123.45;

sprintf(empheader, "%.2f", totalwage);

If you don't like /* 100.0/ proposed ealier you can always use:

int ret = sprintf(empheader, "%.2f", totalwage);
if (ret>0) {
empheader[ret-2] = empheader[ret-1];
empheader[ret-1] = empheader[ret ];
empheader[ret-1] = 0;
}

(plus you should always use snprintf() instead of sprintf().)
 
N

Neil

Chad said:
I would like to remove the decimal but still show the cents using
sprintf.


char empheader[72];
double totalwage = 123.45;


sprintf(empheader, "%.2f", totalwage);


This formats totalwage to show 123.45, I want it to show 12345. I'm
creating an electronic file for wage reporting and the decimal value is
assumed so the dot shouldn't be displayed. Can this be done with
sprintf?

You realize that double will give rounding errors both ways(up and
down). long or longlong ans cents or tenths of cents would solve your
problem and the issues with floats.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top