954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Decimal Places

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

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

Could someone please give me a small example.

Thankyou, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

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

sarehu
Posting Whiz in Training
289 posts since Oct 2007
Reputation Points: 98
Solved Threads: 22
 

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

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Using 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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 
- 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?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You