473,396 Members | 1,840 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,396 software developers and data experts.

print up to n decimals

Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

thanks

Jul 27 '06 #1
4 6283
Gary Wessle wrote:
in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"
Yes. See 'fixed', 'setprecision' and other I/O manipulators (the
header <iomanip>).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #2
Gary Wessle wrote:
in C++, is there a way to print out the float value up to a given
decimal places?
Yes.
say 1/3 to .3333333 "7 decimals"
Look up stream manipulators (in header <iomanip>) in your favorite C++
reference book.

Best regards,

Tom

Jul 27 '06 #3

"Gary Wessle" <ph****@yahoo.comwrote in message
news:87************@localhost.localdomain...
Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"
#include <ios>
#include <iomanip>
#include <iostream>

int main()
{
double d(0);
d = 1.0 / 3.0;

std::cout << std::fixed << std::setprecision(7)
<< d << '\n';

return 0;
}

Output:

0.3333333

-Mike
Jul 27 '06 #4
Mike Wahler wrote:
"Gary Wessle" <ph****@yahoo.comwrote in message
news:87************@localhost.localdomain...
>Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

#include <ios>
#include <iomanip>
[..]
Give a man a fish... Good if he can figure out how you caught it...
Jul 27 '06 #5

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

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
0
by: thx | last post by:
I have MySQL 4.0.18 on Windows 2000 and I have a problem with floats and decimals. The table has floats and decimals fields but when I enter non-integers in these fields, they get rounded. So if I...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
1
by: Bhanu | last post by:
I have a problem in a piece of C Program. I have to print a number of size 19 digit (No decimals). What data type should I use to store and print this no. I have already tried using double, long...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
9
by: Summercoolness | last post by:
it seems that the behavior of "print" is that it will round off properly for any floating point imperfection, such as shown in the first two samples. The third sample seems to be a bug? It...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
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...
0
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...
0
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...
0
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,...

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.