473,387 Members | 1,611 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.

Write 3 decimal places always to stream

How can I write 3 decimal places always to stream?

For example, I wrote:
const double d1 = 1.77777777;
const double d2 = 12.7777777;
const double d3 = 123.777777;

cout << "d1 = " << setw(3) << d1 << endl;
cout << "d2 = " << setw(3) << d2 << endl;
cout << "d3 = " << setw(3) << d3 << endl;

I get this
d1 = 1.77
d2 = 12.7
d3 = 123,

but I want this:

d1 = 1.778
d2 = 12.778
d3 = 123.778

How can I do it with stream (using printf, you would use %.3f)?

Thank you.

Aug 8 '06 #1
3 1795
mo********@yahoo.com wrote:
How can I write 3 decimal places always to stream?

For example, I wrote:
const double d1 = 1.77777777;
const double d2 = 12.7777777;
const double d3 = 123.777777;

cout << "d1 = " << setw(3) << d1 << endl;
cout << "d2 = " << setw(3) << d2 << endl;
cout << "d3 = " << setw(3) << d3 << endl;

I get this
d1 = 1.77
d2 = 12.7
d3 = 123,

but I want this:

d1 = 1.778
d2 = 12.778
d3 = 123.778

How can I do it with stream (using printf, you would use %.3f)?
See 'setprecision'. What book are you reading that does describe
'setw' but doesn't describe 'setprecision'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2

Victor Bazarov wrote:
mo********@yahoo.com wrote:
See 'setprecision'. What book are you reading that does describe
'setw' but doesn't describe 'setprecision'?
thank you, it works. I was reading the VC++ help files and they have
setw() and setfill() but not precision.

Aug 8 '06 #3
mo********@yahoo.com wrote:
Victor Bazarov wrote:
>mo********@yahoo.com wrote:
See 'setprecision'. What book are you reading that does describe
'setw' but doesn't describe 'setprecision'?

thank you, it works. I was reading the VC++ help files and they have
setw() and setfill() but not precision.
Reminds me of an old joke...
"- You know that Pavarotti?.. Nothing special, I tell you."
"- Well... Did you hear him in a movie or a tape recording?"
"- Nah... My neighbor Johnny sang me a few parts."

Get yourself a decent book, and only resort to reading VC++ help files
when you need help on VC++ itself, like IDE, compiler options, etc.
Aug 8 '06 #4

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...
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!
3
by: android | last post by:
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out the number of decimal places I have written a...
6
by: Jim Stools | last post by:
How do I do this: result needs to be a decimal to 2 places? I get casting problems on the below: double myPrice = 1200.125;double myShares = 354.288; decimal myTotal; myTotal = myPrice *...
7
by: capnwhit | last post by:
Hello all, The program below has a bug... The program is supposed to convert a string to a continuous hex dump and back to a string. The output should be as follows: abcd 61626364 abcd
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
2
by: Rosco | last post by:
Problem: Using the sub routing below, when adding a value to another value the results eventually change from 2-decimal places to multiple decimal places. Basically, the amount stored should...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.