473,796 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove decimal but show cents with sprintf

I would like to remove the decimal but still show the cents using
sprintf.
char empheader[72];
double totalwage = 123.45;
sprintf(emphead er, "%.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
6 3120

Chad wrote:
I would like to remove the decimal but still show the cents using
sprintf.
char empheader[72];
double totalwage = 123.45;
sprintf(emphead er, "%.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

Chad wrote:
I would like to remove the decimal but still show the cents using
sprintf.
char empheader[72];
double totalwage = 123.45;
sprintf(emphead er, "%.2f", totalwage);
If you must...

snprintf(emphea der, sizeof(empheade r), "%d", (int)((totalwag e -
floor(totalwage )) * 100));

Tom

Aug 22 '06 #3


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(emphe ader, "%.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
Eric Sosman <Er*********@su n.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
"Chad" <c3***@yahoo.co mwrites:
char empheader[72];
double totalwage = 123.45;

sprintf(emphead er, "%.2f", totalwage);
If you don't like /* 100.0/ proposed ealier you can always use:

int ret = sprintf(emphead er, "%.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*jab ber.org>--ooO--(_)--Ooo--
Aug 23 '06 #6
Chad wrote:
I would like to remove the decimal but still show the cents using
sprintf.
char empheader[72];
double totalwage = 123.45;
sprintf(emphead er, "%.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4540
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
14019
by: Tharma | last post by:
Hi I wanted to extract the decimal portion from a number. I tried the following code but I didn't get the exact decimal portion. If anyone know how to extract please let me know. $num = 1234.56; $decimal = ($num - int($num)); print $decimal;
0
6373
by: John Hicks | last post by:
Is there an accepted best practice on whether to store decimal currency amounts (e.g. dollars and cents) in MySQL decimal column types? Certainly, the most straightforward way is to use decimal columns. But it appears that such values are stored as ASCII strings, which would be inefficient for calculations (requiring conversion to a numeric type for each calculation).
7
13086
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
4
11656
by: Kun | last post by:
i have an html/cgi input that takes in values to a mysql database, however, if i stick in $20 instead of 20, it crashes the program because of the extra $ sign. I was wondering if anyone has a quick regular expression in python to remove the $-sign if it is present in the input.
11
2972
by: MikeY | last post by:
Hi Everyone, I'm trying to extract my pricing from my Access.mdb. Developing WinForm C#. I've read previous posts on the subject, but I'm still confused. In Access my "Price" is of "Currency" type. I extract my items "myDataItem.MyPrice(Convert.ToDecimal(myDataReader)); Now my problem like so many others is that if my dollar value is $10.00, and when I try to extract my data, it converts some how to just "10". How do I
14
14311
by: me2 | last post by:
I am writing a little base conversion utility called base.c. This is what base does. $ base -127 Signed decimal: -127 Unsigned decimal: 4294967169 Hexidecimal: 0xffffff81 Octal: O37777777601 Binary: 1098 7654 3210 9876 5432 1098 7654 3210
0
1187
by: ClassicNancy | last post by:
If I want to remove the drop down for the year will that make the birthday not show on the calendar? If it looks like it is ok to remove it what can be removed?? What should it look like? function date_selection($default_date="", $field_names=array("user_birthday_month", "user_birthday_day", "user_birthday_year"), $labels=1) // Returns date selection as a drop-down menu, if field names are omitted birthday fields are used { //...
1
2058
by: 49erfan21 | last post by:
Edit: Ah, never mind. I feel silly. I just needed to convert amount into an integer. I am having a little trouble figuring out how to assign a variable (dollars) to the numbers left of the decimal and how to assign a variable (cents) to the right of the decimal. The only parts of the code I can modify are those 2 assignment statements because it is a homework question. The user inputs something like 123.45 Thanks for any help. import...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.