I am trying to get quite a large number 0.00012354684 to X required decimal places.

Using <iomanip> and setprecision() but I cant seem to get it to work.

Could someone please give me a small example.

Thankyou, Regards X

Recommended Answers

All 6 Replies

How large is X? What is the type of your large number? 0.00012354584 is actually quite small :D

number = 0.00012354684
type = float
decimal points vary = 1-5

Use double (~16 decimal digits); 32-bit floats keep only ~5-7 decimal digits.
Always use double type in math calculations!

Using <iomanip> and setprecision() but I cant seem to get it to work.

Could someone please give me a small example.

ok:

float a = 0.00012354684; 
    //number has 8 meaningfull digits (skip the first zero's)
    cout <<  setprecision (8) << a << "\n";
    cout <<  setprecision (5) << a << "\n";
    cout <<  setprecision (3) << a << "\n";
    cout <<  setprecision (1) << a << "\n";

output:

0.00012354684
0.00012355
0.000124
0.0001

What doesn't work in your program? Post your code.

Nice work niek_e again!

I found my problem:
- is there something I should be aware of when dividing 2 numbers in C++?
- thats why I am getting no decimal points?

Another small example, please? :)

Thankyou, Regards X

- is there something I should be aware of when dividing 2 numbers in C++?
- thats why I am getting no decimal points?

try this:

float a = 1.2, b = 0.5;
    int c = 2, d = 3;
    cout << "two floats: " << a/b << "\n"; 
    cout << "two ints: " << d/c << "\n"; 
    cout << "float and int: " << a/c << "\n";

output

2.4
1
0.6

so the answer is: yes. Dividing 2 ints will result in loss of decimal point. But that's kind of obvious right?

commented: Thanks for the hints +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.