Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 7th, 2007, 12:15 AM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
float - cut down digts?

you know if you do float x = 12/12.0;
it'll give you x = 1.00000

now what do i do if i want to cout the result at 1.0

do I have to convert x into a string and take out "characters" or what? that just doesnt sound very sophiscated
i feel there is better approach to it?
programmingnoob is offline   Reply With Quote
Old Feb 7th, 2007, 1:39 AM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Try this:
std::cout << std::setprecision(1) << x <<std::endl;
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Feb 9th, 2007, 5:09 PM   #3
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
That's fairly tedious, though. Isn't there a quicker way, aside from the C-style printf()?
UnKnown X is offline   Reply With Quote
Old Feb 9th, 2007, 5:58 PM   #4
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Boost.Format offers printf() style with type-safety.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Feb 11th, 2007, 3:06 PM   #5
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Quote:
Originally Posted by UnKnown X View Post
That's fairly tedious, though. Isn't there a quicker way, aside from the C-style printf()?
yeah...at the top of the program justa fter those std headers go:
using namespace std;

and then go:
cout << setprecision(1) << x << endl;



an even easier approach is coding your own fraction printing function.
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Feb 11th, 2007, 3:13 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"using namespace std;" is really for small exemplar programs and isn't really a good recommendation for robust programming of a serious nature. It introduces a lot of symbols; in a large program, you're likely to duplicate some (how many 'max' labels have you seen introduced by the coder). One alternative to always qualifying the label is to introduce just a few commonly used members, such as
using std::cout;
using std::cin;
using std::string;
etc.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 11th, 2007, 7:01 PM   #7
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
do what dawei said. i'm drunk right now.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja 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
How do I take a square root? Fall Back Son C 26 Oct 23rd, 2006 12:24 PM
converting string to float beginnerCCC C 22 Oct 2nd, 2006 11:59 PM
PLEASE HELP Convert FLOAT to STRING Pedro C 2 May 23rd, 2005 10:55 AM
Two ints divided into a float. Dygear C++ 2 Apr 1st, 2005 5:42 PM
float precision and MFC... Racine_ C++ 0 Feb 5th, 2005 8:31 AM




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

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