User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,788 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,788 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 363 | Replies: 6 | Solved
Reply
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Decimal Places

  #1  
Jul 17th, 2008
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
"You never stop learning" - OmniX
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cambridge, MA
Posts: 247
Reputation: sarehu is on a distinguished road 
Rep Power: 1
Solved Threads: 21
sarehu's Avatar
sarehu sarehu is offline Offline
Posting Whiz in Training

Re: Decimal Places

  #2  
Jul 18th, 2008
How large is X? What is the type of your large number? 0.00012354584 is actually quite small
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Decimal Places

  #3  
Jul 18th, 2008
number = 0.00012354684
type = float
decimal points vary = 1-5
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Jul 2008
Posts: 385
Reputation: ArkM is a jewel in the rough ArkM is a jewel in the rough ArkM is a jewel in the rough 
Rep Power: 3
Solved Threads: 57
ArkM ArkM is online now Online
Posting Whiz

Re: Decimal Places

  #4  
Jul 18th, 2008
Use double (~16 decimal digits); 32-bit floats keep only ~5-7 decimal digits.
Always use double type in math calculations!
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,630
Reputation: niek_e is just really nice niek_e is just really nice niek_e is just really nice niek_e is just really nice 
Rep Power: 8
Solved Threads: 168
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Decimal Places

  #5  
Jul 18th, 2008
Originally Posted by OmniX View Post
Using <iomanip> and setprecision() but I cant seem to get it to work.

Could someone please give me a small example.


ok:

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

output:
0.00012354684
0.00012355
0.000124
0.0001

What doesn't work in your program? Post your code.
Last edited by niek_e : Jul 18th, 2008 at 5:27 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Decimal Places

  #6  
Jul 18th, 2008
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
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,630
Reputation: niek_e is just really nice niek_e is just really nice niek_e is just really nice niek_e is just really nice 
Rep Power: 8
Solved Threads: 168
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Decimal Places

  #7  
Jul 18th, 2008
Originally Posted by OmniX View Post
- is there something I should be aware of when dividing 2 numbers in C++?
- thats why I am getting no decimal points?


try this:
  1. float a = 1.2, b = 0.5;
  2. int c = 2, d = 3;
  3. cout << "two floats: " << a/b << "\n";
  4. cout << "two ints: " << d/c << "\n";
  5. 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?
Last edited by niek_e : Jul 18th, 2008 at 5:55 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC