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 361,561 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,041 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:
Views: 13973 | Replies: 3 | Solved
Reply
Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Double values with two decimal places

  #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.
// 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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Double values with two decimal places

  #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  
Join Date: Jan 2005
Posts: 43
Reputation: Siersan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Double values with two decimal places

  #3  
Feb 3rd, 2005
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;
}
Reply With Quote  
Join Date: Feb 2005
Posts: 12
Reputation: Sam Lehman is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sam Lehman Sam Lehman is offline Offline
Newbie Poster

Re: Double values with two decimal places

  #4  
Feb 3rd, 2005
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.
Reply With Quote  
Reply

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

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

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