![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Try this:
std::cout << std::setprecision(1) << x <<std::endl;
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
That's fairly tedious, though. Isn't there a quicker way, aside from the C-style printf()?
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
Boost.Format offers printf() style with type-safety.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
Quote:
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! |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"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 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I take a square root? | Fall Back Son | C | 26 | Oct 23rd, 2006 1:24 PM |
| converting string to float | beginnerCCC | C | 22 | Oct 3rd, 2006 12:59 AM |
| PLEASE HELP Convert FLOAT to STRING | Pedro | C | 2 | May 23rd, 2005 11:55 AM |
| Two ints divided into a float. | Dygear | C++ | 2 | Apr 1st, 2005 6:42 PM |
| float precision and MFC... | Racine_ | C++ | 0 | Feb 5th, 2005 9:31 AM |