| | |
Double values with two decimal places
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2005
Posts: 12
Reputation:
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.
C++ Syntax (Toggle Plain Text)
// 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; }
Is there something wrong with this?
C++ Syntax (Toggle Plain Text)
#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:
Solved Threads: 0
•
•
•
•
Originally Posted by Siersan
Is there something wrong with this?
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
- decimal places & asterisk help (C#)
- Decimal places-Need Help (Java)
- Need More Than 5 Decimal Places!! (C++)
- C++ question(Decimal places)very weird (C++)
Other Threads in the C++ Forum
- Previous Thread: operator overloading
- Next Thread: reading txt file into array
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





