Double values with two decimal places

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Double values with two decimal places

 
0
  #1
Feb 3rd, 2005
hi, i've got a bit of a roadbump in my work.

I need to set all of the money values to have two decimal points on the end, but my current program only adds two places if there are more than one.
right now, i am multiplying the value by 100 and truncating with an int and dividing by 100.

for example, the number 387.3532 would become 387.35, but the number 440 would stay at 440 (instead of what i want: 440.00)

anyone have any ideas?

here is my current code:
sample inputs: 44, 10, 1, 1, 44, 10, 0, 1, 33, 10, 0.
  1. // This is the main project file for VC++ application project
  2. // generated using an Application Wizard.
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #using <mscorlib.dll>
  7. using namespace System;
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int _tmain(){
  12. cout<<" ************************************************************\n";
  13. cout<<"** Welcome to the Weekly Payroll Program Thing! **\n";
  14. cout<<"** **\n";
  15. cout<<"** Written by Sam Lehman **\n";
  16. cout<<" ************************************************************\n";
  17. cout<<"\n";
  18.  
  19. int empCounter=1, anotherRound, exempt;
  20. double rate, moneyCounter=0, hours, empMoney, otHours, otPay, scale = pow(10.0, 2);;
  21.  
  22.  
  23. while (true){
  24. // input and such...
  25. hours = 0; rate = 0; exempt=-1, otHours=0, otPay=0;
  26. cout<<" How many hours did this the employee work? \n ";
  27. cin>>hours;
  28. cout<<" What is this employee's hourly rate? \n ";
  29. cin>>rate;
  30. if (hours > 40){
  31. cout<<" Is this employee exempt? (press 1 for yes or 0 for no)\n ";
  32. cin>>exempt;
  33. if (exempt != 1){
  34. otHours = hours - 40;
  35. otPay = otHours * rate * 1.5;
  36. }else{
  37. otHours = hours - 40;
  38. otPay = otHours * rate * 1;
  39. }
  40. }
  41.  
  42. // calculations and shit..
  43. empMoney = ((hours - otHours)*rate) + otPay;
  44. moneyCounter+=empMoney;
  45.  
  46.  
  47. rate = (int)(rate * scale) / scale;
  48. otPay = (int)(otPay * scale) / scale;
  49. empMoney = (int)(empMoney * scale) / scale;
  50.  
  51.  
  52. // output the current employee's info.
  53. cout<<"\n Employee Number: "<<empCounter;
  54. cout<<"\n Hours: "<<hours;
  55. cout<<"\n Rate: $"<<rate;
  56. if (exempt != -1) cout<<"\n Exempt: "<<exempt;
  57. else cout<<"\n Exempt: ";
  58.  
  59. if (otPay != 0) cout<<"\n OT Pay: $"<<otPay;
  60. else cout<<"\n OT Pay: ";
  61. cout<<"\n Gross Pay: $"<<empMoney<<"\n";
  62.  
  63. //process another?
  64. cout<<"\n Would you like to calculate another employee? \n (press 1 for yes or 0 for no)\n ";
  65. cin>>anotherRound;
  66. if (anotherRound == 0){
  67. moneyCounter = (int)(moneyCounter * scale) / scale;
  68. cout<<"Total pay for "<<empCounter<<" employees is $"<<moneyCounter<<endl;
  69. break;
  70. } else empCounter++;
  71. }
  72. return 0;
  73. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Double values with two decimal places

 
0
  #2
Feb 3rd, 2005
If it were me, I'd use sprintf() or one of its variants (_snprintf, say).

char display[30];
_snprintf( display, sizeof(display), "%.2f", empMoney );

And forget about scaleing it manually.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Double values with two decimal places

 
1
  #3
Feb 3rd, 2005
Is there something wrong with this?
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. double a = 440;
  9. double b = 387.3532;
  10.  
  11. cout.setf(ios::fixed);
  12. cout<< setprecision(2) << a <<'\n';
  13. cout<< setprecision(2) << b <<endl;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Re: Double values with two decimal places

 
0
  #4
Feb 3rd, 2005
Originally Posted by Siersan
Is there something wrong with this?
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. double a = 440;
  9. double b = 387.3532;
  10.  
  11. cout.setf(ios::fixed);
  12. cout<< setprecision(2) << a <<'\n';
  13. cout<< setprecision(2) << b <<endl;
  14. }

thanks a lot, that works great.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC