•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,449 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,889 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.
Views: 14661 | Replies: 3 | Solved
![]() |
| |
•
•
Join Date: Feb 2005
Posts: 12
Reputation:
Rep Power: 4
Solved Threads: 0
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.
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.
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#include <math.h>
#using <mscorlib.dll>
using namespace System;
#include <iostream>
using namespace std;
int _tmain(){
cout<<" ************************************************************\n";
cout<<"** Welcome to the Weekly Payroll Program Thing! **\n";
cout<<"** **\n";
cout<<"** Written by Sam Lehman **\n";
cout<<" ************************************************************\n";
cout<<"\n";
int empCounter=1, anotherRound, exempt;
double rate, moneyCounter=0, hours, empMoney, otHours, otPay, scale = pow(10.0, 2);;
while (true){
// input and such...
hours = 0; rate = 0; exempt=-1, otHours=0, otPay=0;
cout<<" How many hours did this the employee work? \n ";
cin>>hours;
cout<<" What is this employee's hourly rate? \n ";
cin>>rate;
if (hours > 40){
cout<<" Is this employee exempt? (press 1 for yes or 0 for no)\n ";
cin>>exempt;
if (exempt != 1){
otHours = hours - 40;
otPay = otHours * rate * 1.5;
}else{
otHours = hours - 40;
otPay = otHours * rate * 1;
}
}
// calculations and shit..
empMoney = ((hours - otHours)*rate) + otPay;
moneyCounter+=empMoney;
rate = (int)(rate * scale) / scale;
otPay = (int)(otPay * scale) / scale;
empMoney = (int)(empMoney * scale) / scale;
// output the current employee's info.
cout<<"\n Employee Number: "<<empCounter;
cout<<"\n Hours: "<<hours;
cout<<"\n Rate: $"<<rate;
if (exempt != -1) cout<<"\n Exempt: "<<exempt;
else cout<<"\n Exempt: ";
if (otPay != 0) cout<<"\n OT Pay: $"<<otPay;
else cout<<"\n OT Pay: ";
cout<<"\n Gross Pay: $"<<empMoney<<"\n";
//process another?
cout<<"\n Would you like to calculate another employee? \n (press 1 for yes or 0 for no)\n ";
cin>>anotherRound;
if (anotherRound == 0){
moneyCounter = (int)(moneyCounter * scale) / scale;
cout<<"Total pay for "<<empCounter<<" employees is $"<<moneyCounter<<endl;
break;
} else empCounter++;
}
return 0;
}•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
Is there something wrong with this?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double a = 440;
double b = 387.3532;
cout.setf(ios::fixed);
cout<< setprecision(2) << a <<'\n';
cout<< setprecision(2) << b <<endl;
}•
•
Join Date: Feb 2005
Posts: 12
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by Siersan
Is there something wrong with this?
#include <iomanip> #include <iostream> using namespace std; int main() { double a = 440; double b = 387.3532; cout.setf(ios::fixed); cout<< setprecision(2) << a <<'\n'; cout<< setprecision(2) << b <<endl; }
thanks a lot, that works great.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Two Decimal Places (VB.NET)
- problem with decimal places (Visual Basic 4 / 5 / 6)
- C++ question(Decimal places)very weird (C++)
- Is there a stream class member equivalent to sprintf() (C)
- Excel97 decimal places (Windows Software)
- setting the number of decimal points in double varibale (C)
Other Threads in the C++ Forum
- Previous Thread: operator overloading
- Next Thread: reading txt file into array


Hybrid Mode