473,805 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20084
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_b ase::fixed, ios_base::float field);
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
6159
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
3
7095
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 the number has 2 decimal places. Any help would be great. Regards, Brent
2
3606
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 can't figure out why the design is so that the DataRow "alters" the value entered. In my application a decimal column in a row of a specific table has a fix number of decimal places according to certain premises. The
7
1615
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 numbers two decimal places long like 10.01 I only know how to define numbers as int or double. Do I use float? Also, I'm using Visual Studio .NET is there anyway to keep the compiler on the screen long enough to actually see what it's outputting. ...
17
2351
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 displayed is $9.94 ! If you add ANYTHING else the total is correct, 9.95 plus whatever you add. That is the only price in these samples that is doing that. All the others display properly. If you change the 9.95 to ANY other number it displays...
4
9736
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 even after I have change the Fieldsize propert to Decimal with Scale = 2, the digits after the decimal are not seen. For eg. If the text was 16.27. After I changed to long integer, it
8
11331
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
2778
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 0.220 it comes out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1. How can I format it to include the zeros?
3
1994
by: David | last post by:
Any ideas why this statement: mBalanceChange = Double.Parse(strCurrentBalance, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble would leave me with a double containing over 10 decimal places when strCurrentBalance was a string containing a double with two decimal places and mBalanceDouble contained only two decimal places to begin with?
0
9716
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
9596
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
10360
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...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
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
6876
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
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
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
3845
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.