943,476 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 42720
  • C++ RSS
Feb 3rd, 2005
0

Double values with two decimal places

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sam Lehman is offline Offline
12 posts
since Feb 2005
Feb 3rd, 2005
0

Re: Double values with two decimal places

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Feb 3rd, 2005
1

Re: Double values with two decimal places

Is there something wrong with this?
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 2
Light Poster
Siersan is offline Offline
45 posts
since Jan 2005
Feb 3rd, 2005
0

Re: Double values with two decimal places

Quote originally posted by Siersan ...
Is there something wrong with this?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sam Lehman is offline Offline
12 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: operator overloading
Next Thread in C++ Forum Timeline: I just need help starting this program please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC