473,766 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting decimal places

I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.
Jul 22 '05 #1
36 7827
Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
news:8e******** *************** ***@posting.goo gle.com...
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.


Check out boost::format.

Marcin
Jul 22 '05 #2

"Andrew" <ad*****@hqcnsg .navy.mil> wrote in message
news:8e******** *************** ***@posting.goo gle.com...
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.


You have to set the 'float mode' to fixed. E.g.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

cout << fixed << setprecision(1) << 1.23456789 << '\n';

cout << fixed << setprecision(2) << 1.23456789 << '\n';

cout << fixed << setprecision(3) << 1.23456789 << '\n';

cout << fixed << setprecision(4) << 1.23456789 << '\n';

cout << fixed << setprecision(5) << 1.23456789 << '\n';

}

john


Jul 22 '05 #3
Marcin Kalicinski wrote:

Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
news:8e******** *************** ***@posting.goo gle.com...
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.


Check out boost::format.

Marcin


Boost is OT in this forum.
Jul 22 '05 #4

"Andrew" <ad*****@hqcnsg .navy.mil> wrote in message
news:8e******** *************** ***@posting.goo gle.com...
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.


#include <ios>
#include <iomanip>
#include <iostream>

int main()
{
double d(3.14);

std::cout << std::fixed << std::setprecisi on(4)
<< d << '\n';

return 0;
}

Output:

3.1400
-Mike

Jul 22 '05 #5
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c63g5t$7da a2$1@ID-
You have to set the 'float mode' to fixed. E.g. cout << fixed << setprecision(1) << 1.23456789 << '\n';

cout << fixed << setprecision(2) << 1.23456789 << '\n';


The float mode is sticky, so you don't have to set it in each line, though
setting it is harmless and often a good idea for clarity if we're fixing
fixed and scientific output in the same program.

cout << fixed;
cout << setprecision(1) << 1.23456789 << '\n';
cout << setprecision(2) << 1.23456789 << '\n';
Jul 22 '05 #6
Julie <ju***@nospam.c om> wrote in news:40******** *******@nospam. com:
Marcin Kalicinski wrote:

Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
news:8e******** *************** ***@posting.goo gle.com...
> I trying to format my output to display a set number of decimal
> places. I have been trying to use the <iomanip> setprecision(), but
> that will only display the total number of digits. Can someone please
> help me???? Thanks.


Check out boost::format.

Marcin


Boost is OT in this forum.


Why don't you like boost?

Strange, but I've seen other people not using it *just because* (i'm not
counting compiler issues that might arise with boost).
Why - I wonder.

cheers!
b
Jul 22 '05 #7
bartek wrote:

Julie <ju***@nospam.c om> wrote in news:40******** *******@nospam. com:
Marcin Kalicinski wrote:

Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
news:8e******** *************** ***@posting.goo gle.com...
> I trying to format my output to display a set number of decimal
> places. I have been trying to use the <iomanip> setprecision(), but
> that will only display the total number of digits. Can someone please
> help me???? Thanks.

Check out boost::format.

Marcin


Boost is OT in this forum.


Why don't you like boost?


I did not express my preference for Boost in my response.

I was expressing (rather tersely) that a reference to Boost is off-topic in
this forum when there is an existing and preferred solution in C++ language.
Jul 22 '05 #8
Julie <ju***@nospam.c om> wrote in news:40******** *******@nospam. com:
bartek wrote:

Julie <ju***@nospam.c om> wrote in news:40******** *******@nospam. com:
> Marcin Kalicinski wrote:
>>
>> Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
>> news:8e******** *************** ***@posting.goo gle.com...
>> > I trying to format my output to display a set number of decimal
>> > places. I have been trying to use the <iomanip> setprecision(),
>> > but that will only display the total number of digits. Can
>> > someone please help me???? Thanks.
>>
>> Check out boost::format.
>>
>> Marcin
>
> Boost is OT in this forum.


Why don't you like boost?


I did not express my preference for Boost in my response.

I was expressing (rather tersely) that a reference to Boost is
off-topic in this forum when there is an existing and preferred
solution in C++ language.


Sorry, I overreacted to your post.

Although, I don't think that mentioning boost is off topic here at all.
Just as it's not off topic to direct an (confused?) individual to other
(more appropriate?) newsgroups, isn't it?

cheers!
b
Jul 22 '05 #9
On Tue, 20 Apr 2004 08:45:50 -0700 in comp.lang.c++, Julie
<ju***@nospam.c om> wrote,
Marcin Kalicinski wrote:

Uzytkownik "Andrew" <ad*****@hqcnsg .navy.mil> napisal w wiadomosci
news:8e******** *************** ***@posting.goo gle.com...
> I trying to format my output to display a set number of decimal
> places. I have been trying to use the <iomanip> setprecision(), but
> that will only display the total number of digits. Can someone please
> help me???? Thanks.


Check out boost::format.

Marcin


Boost is OT in this forum.


Where OT = "On Topic"

Jul 22 '05 #10

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

Similar topics

0
2706
by: ian | last post by:
Hi guys, i'm trying to format the output of a record in access, which i am pulling from through php (odbc) and the formatting is set at 4 decimal places, which i don't want. i want currency and no decimal places. I was trying to use this example, but i have no bloody idea howo to use it. or where to put it for that matter: <?php $number = 1234.56;
17
6149
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...
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!
7
12700
by: Chris | last post by:
Where can I find a fairly comprehensive list of the formatting codes used when data binding. e.g. <%# Bind("Debit", "{0:C}") %>. Specifically formatting a number to 2 decimal places. Regards, Chris.
17
5285
by: scan87 | last post by:
Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the...
4
1805
by: sparks | last post by:
In this database some of the text boxes need to be/show something like 88.9. in the table I marked them single, format 00.0 and decimal places 1 on the form I have fixed and 1 if someone types in 88.9 its fine....if they type in 88.99 it gets displayed as 89.0 but in the table its 88.99 when you click on the value in the table. I wanted to do something like ##.# so they had to put in the correct
3
2502
by: Nathan Sokalski | last post by:
I am using databinding to populate a dropdownlist with numeric values. Some of the values have more decimal places than others, and I only want the minimal number of decimal places necessary displayed. The only way I have found to do this is to use the following for the DataTextFormatString property: "{0:0.######}" This displays what I want for now, but if the number had more that 6 decimal places, it would not work. Is there any way...
9
2321
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical, and hex but these do not seem to allow for specifying the number of decimals, left/right placement, or string formatting. Thanks
2
11915
Pittaman
by: Pittaman | last post by:
Hello I am creating some crystal reports (for visual studio 2005) based on the content of certain .NET objects. I'm doing this in .NET 2.0. For one of them I'm using a Cross-table to summarize the information of a bunch of objects. The actual data is numeric. Since these reports are meant to be flexible number formatting must be configured as defined by the requirements. For example, sometimes the values will have to be rounded to 5...
0
9571
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
9404
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
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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,...
0
9838
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...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
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
5279
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.