473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with outputting 2 decimal places...

I'm trying to output 2 decimal places to the screen even when the last
digit would be 0. Here is what I have now:

#include <iostream.h>
#include <iomanip.h>

double custname=0;

void main()
{
custname=12.5;
cout << setprecision(4) << custname;
}

When I set custname to a value like 12.59 it prints 12.59. But the above
only prints 12.5. I've searched and tried several different ways on Google
and nothing seems to work.

Just so you know, I'm just starting out with C++. Thanks in advance for
any help anyone can provide.

Dave
--
You can talk about us, but you can't talk without us!
US Army Signal Corps!!
http://www.geocities.com/davidcasey98

Remove "IH8SPAM" to reply!
Jul 22 '05 #1
2 20052
David Casey wrote:
I'm trying to output 2 decimal places to the screen even when the last
digit would be 0. Here is what I have now:

#include <iostream.h>
#include <iomanip.h>

double custname=0;

void main()
{
custname=12.5;
cout << setprecision(4) << custname;
}

When I set custname to a value like 12.59 it prints 12.59. But the above
only prints 12.5. I've searched and tried several different ways on Google
and nothing seems to work.

Just so you know, I'm just starting out with C++. Thanks in advance for
any help anyone can provide.

Dave


1. You're using old headers.
2. use showpoint or fixed
3. Main returns int

#include <iostream>
#include <iomanip>

using namespace std;

double custname = 0;
int main()
{
custname = 12.5;
cout << setprecision(4) << showpoint << custname << endl;
cout << setprecision(4) << fixed << custname << endl;
return 0;
}
generates:

12.50
12.5000
}
Jul 22 '05 #2
On Fri, 20 Feb 2004 00:41:57 GMT, red floyd wrote in comp.lang.c++:
1. You're using old headers.
Yes, fixed that now. :-)
2. use showpoint or fixed
I hadn't heard of those commands. The teacher was telling us to just put
cout.precision(2) right inside of main, but that wasn't working.
3. Main returns int
Yes, I know but the teacher hasn't "shown" us that part yet. I know a
little more than the teacher is telling us because it's just a programming
logic class and I've done a little reading on my own regarding C++. I
didn't want to use stuff the teacher hasn't shown us in class.
#include <iostream>
#include <iomanip>

using namespace std;

double custname = 0;
int main()
{
custname = 12.5;
cout << setprecision(4) << showpoint << custname << endl;
cout << setprecision(4) << fixed << custname << endl;
return 0;
}
generates:

12.50
12.5000
}


I just gave this a shot and it worked good:

#include <iostream>
#include <iomanip>

using namespace std;

float custname=0;
float roomnum=0;

void main()
{
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(2);
cout << custname << endl;
cout << roomnum << endl;
}

It also seems to toggle on the 2 decimal place precision which is what I
was looking for and I think the teacher meant but I don't know why he
didn't tell us about the cout.setf line. I also figured out that without
the using namespace std; line, it doesn't work either and that's also
something the teacher hasn't told us to use so again, I wasn't going to go
over what he was already showing us. However, in this case I think I'll
just use it (like I have a choice).

Thanks for the help, though. It gives me another choice to use. :-)

Dave
--
You can talk about us, but you can't talk without us!
US Army Signal Corps!!
http://www.geocities.com/davidcasey98

Remove "IH8SPAM" to reply!
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
2
by: Carl G | last post by:
I am storing a 0.000 a System.Decimal in a DataRow. On retrieval the value is only 0 without the three decimal places. It looks like the Get property returns System.Decimal.Zero, but why???? I...
7
by: BobJohnson | last post by:
Just started learning C++ and I need some help with my homework, shouldn't take long for people around here. I need to create a simple money calculator but I don't know how to make the output...
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
4
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
8
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
2
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output...
3
by: David | last post by:
Any ideas why this statement: mBalanceChange = Double.Parse(strCurrentBalance, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.