Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > number of decimal places

Reply
Thread Tools

number of decimal places

 
 
shez
Guest
Posts: n/a
 
      01-20-2005
Hi,

Is there any way to output a double value with different number of
decimal places (apart from using 'sprintf'). I looked up the reference
for C++ streams and found the 'setprecision' manipulator, but this sets
the number of digits before and after the decimal point. I can't seem
to find any manipulator for setting the number of digits after the
decimal point.

Thanks,
-shez-

 
Reply With Quote
 
 
 
 
shez
Guest
Posts: n/a
 
      01-20-2005
Sorry guys, i've found the answer. Pls ignore.

Sorry,
-shez-

 
Reply With Quote
 
 
 
 
Ulrich Achleitner
Guest
Posts: n/a
 
      01-20-2005
On 19 Jan 2005 19:11:42 -0800, shez <> wrote:

> Sorry guys, i've found the answer. Pls ignore.


you would not need to be sorry, if you were so kind to post the answer you
have found - perhaps others are interested, too.

ps: ignoring, as you suggest, is not possible, because to do so, anybody
must read your original posting, plus your answer to it...

--
have a nice day
ulrich
 
Reply With Quote
 
shez
Guest
Posts: n/a
 
      01-20-2005
> you would not need to be sorry, if you were so kind to post the
answer you
> have found - perhaps others are interested, too.


You can use the 'std::fixed' manipulator before setprecision, like
this:

std::cout << std::fixed << std::setprecision(2) << 1.2345678 <<
std::endl;

This should print to two decimal places. Thanks, -shez-

 
Reply With Quote
 
saurabh.unercat saurabh.unercat is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-19-2011
the above codes work for the newr compilers...if any of you are luking fr things that work with <xyz.h> files only...use the setf as follows


cout.setf(ios::fixed);
cout<<setprecision(4)<<number;


..//this prints exactly 4 digits after a decimal point
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of decimal places in a double harryos Java 4 11-17-2009 09:47 PM
How to tell if a number has decimal places different than 0 Gaijinco C++ 27 09-28-2005 07:58 PM
How to tell if a number has decimal places different than 0 Gaijinco C Programming 29 09-28-2005 07:58 PM
Rounding a Number to a String with N decimal places Dr John Stockton Javascript 1 08-15-2004 06:17 PM
Round up a number to so many decimal places Gizmo C++ 1 08-31-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57