By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,564 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,564 IT Pros & Developers. It's quick & easy.

remove decimal but show cents with sprintf

P: n/a
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?

Aug 22 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Chad wrote:
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 ) );

Aug 22 '06 #2

P: n/a

Chad wrote:
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

Aug 22 '06 #3

P: n/a


Ancient_Hacker wrote On 08/22/06 10:34,:
Chad wrote:
>>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.

--
Er*********@sun.com

Aug 22 '06 #4

P: n/a
Eric Sosman <Er*********@sun.comwrites:
Ancient_Hacker wrote On 08/22/06 10:34,:
>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.
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.)
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Aug 22 '06 #5

P: n/a
"Chad" <c3***@yahoo.comwrites:
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().)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Aug 23 '06 #6

P: n/a
Chad wrote:
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.
Aug 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.