Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 18th, 2008, 6:58 PM   #1
aShoe
Newbie
 
Join Date: Feb 2008
Posts: 1
Rep Power: 0 aShoe is on a distinguished road
limiting decimal places

Can some one give me an idea of how to limit decimal spaces from a double.
aShoe is offline   Reply With Quote
Old Feb 18th, 2008, 8:02 PM   #2
dr.p
Programmer
 
dr.p's Avatar
 
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3 dr.p is on a distinguished road
Re: limiting decimal places

aShoe, if you're using the iostream library for output, you need to use the setprecision() manipulator (http://cppreference.com/io_flags.html#manipulators).

Example:
#include <iostream>
#include <iomanip>
//...
float floatval = 4.026783;
std::cout << std::dec << std::setprecision(3) << floatval << std::endl;
// outputs "4.03"

Or, if you're using C style output:
#include <stdio.h>
//...
float floatval = 4.026783;
printf("%.2f\n", nfloat);
// outputs "4.03"
__________________
Neeley.org
dr.p is offline   Reply With Quote
Old Feb 18th, 2008, 8:05 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 530
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: limiting decimal places

You can only limit them on output, such as with cout of another output stream
double x = 12.1234567890;
cout << setprecision(4) << width(4) << x << "\n";

has the following output
12.12
Press any key to continue . . .

[edit] I was too slow. what dr.p said[/edit]
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can anybody help me about conversion decimal to binary and vice versa.. crigie Assembly 1 Jan 11th, 2008 7:43 PM
Decimal to binary help gam3r Java 20 Nov 21st, 2007 8:57 PM
Java code to convert binary to decimal and hexadecimal to binary prince_haldir Java 1 Mar 7th, 2006 1:51 AM
Decimal conventions JoeAvery Delphi 1 Jan 18th, 2006 3:43 PM
hex and decimal RGB transforming please help cloud- Visual Basic 5 Jan 17th, 2005 3:17 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC