setprecision woes

• 01-27-2003
foo
setprecision woes
Hello, this is my first post here...please bear with me, I'm an amateur at this...

I want to write a program that can display any nonnegative floating point value as a decimal number with exactly two digits to the right of the decimal, because we are dealing with monetary quantities.

This is pretty straightforward, so I have something to the effect of
Code:

```        float pay;         int precision; int main() { ...         cout<<"Enter the pay\n";         cin>>pay;         precision=floor(log10(pay))+3;         cout<<setprecision(precision)<<"\n\nThe pay is "<<pay<<"\n" ... }```
This works in most but not all cases, namely when pay < .01. Even when setprecision is 0 or below it still displays one significant digit (i.e. 0.003). I could get around this by placing a check to display the quantity as 0.00 when precision < 1, I suppose, but then a quantity like 0.009 won't be rounded up to 0.01. Maybe multiply by 100 and round to the nearest integer, then divide by 100 again?

As well, when an integer quantity like 123 is inputted, it is redisplayed as an integer though the value for setprecision will be what I want it to be (5 in this case). I could tack on ".00" when pay is an integer, but that doesn't account for situations where 123.006 can be displayed as 123.01 but 123.004 -- a non integer value -- will be regurgitated as simply 123. Maybe I should follow my own suggestion again, multiply/round/divide with 100.

I have a feeling I'm making this a lot harder than it needs to be or missing something obvious. Maybe I should forgo setprecision altogether, and display the integer quantity and then calculate/display the rounded quantity to the right of the decimal.

Oh well, even if no one responds to this thread, typing it out helped me think the problem through :o
• 01-27-2003
alpha
you could try this, if all the formatting in the whole program is the same.

Code:

```#include <iomanip> int main() {     int number;     std::cout << setprecision(2);     std::cout.setf(ios::showpoint | ios::fixed);     ...     return 0; }```
• 01-27-2003
skipper
alpha is right. ios::fixed is the key to your problem.

setprecision() has "default" settings which are fine as long as you understand how they function. (It doesn't discriminate between the mantissa and decimal value.)

By "invoking" ios::fixed, you can use setprecision() to fix the number of decimal places that you need.

-Skipper
• 01-27-2003
BakAttack
floats
Ok I use an oldschool compiler (borland turbo c++ from 19-digity-2) but here is my solution. also i dont know what alpha used (in terms of std - could some1 explain what it is thx) so here:

Code:

``` #include <iostream.h> #include <iomanip.h> #include <conio.h> int main() {     float pay;     clrscr(); // or void clrscr(); when i try on dev c++     cout.setf(ios::showpoint);     cout.setf(ios::fixed);  // i think this is the same thing he did     cout << "\nPay: ";     cin >> pay;     cout  << endl << setprecision(2) << pay; }```
thats just my solution hope it helps =\
• 01-27-2003
Cela
>>Ok I use an oldschool compiler (borland turbo c++ from 19-digity-2)
Borland's 5.5 compiler is free for download and supports all the good stuff from ISO C++ :-)
• 01-27-2003
BakAttack
Quote:

Borland's 5.5 compiler is free for download and supports all the good stuff from ISO C++ :-)
Really? Wow, thanks alot is it at borland.com or something, ive used this old software because its what we use in class. I usually just play around because the class is WAY too easy so when it comes time for extra credit (sorting array's, tic-tac-toe game) i shine =). BTW could anyone think of a decently challenging project that involves pretty much basic knowledge of pointers and requires good knowledge of classes, good function use, ect. Ive made a base converter, tic-tac-toe game, and some others. Also i wan't to try and make a chess program, and i know recursive functions, so where would be a good place to start.

thx
• 01-27-2003
Cela
>>is it at borland.com or something
Second on the list here. :-)

>>BTW could anyone think of a decently challenging project that involves pretty much basic knowledge of
>>pointers and requires good knowledge of classes, good function use, ect.
How about a little interpreted language? Real simple stuff like
Code:

```~ An interpreted program ~ Interpreter written in C++ {   scalar(a,b,c) := (1,2,3).   c := a + b + c.   write CONSOLE c. }```
• 01-28-2003
alpha
the std:: stuff is new standard, the iostream.h with the .h ending will give you backwards warnings with newer compilers. so you could do one of the following:
Code:

```#include <iostream> using namespace std; int main() { }```
Code:

```#include <iostream> using std::cout; using std::cin; //continue with what needs to be used int main() { }```
Code:

```#include <iostream> int main() {     std::cout << std::endl;     return 0; }```