Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

what's the best way to truncate a floating point number to 2 decimal places to work with in C++?


ghost's Avatar
0 0

I made a cash register program (don't ask..) and I need a way to truncate the floating point numbers to 2 decimal places to more accurately work with the change. more specifically, here is the output of the problem for which I'm asking:

Enter the cost of the first item and hit enter.
Enter 0 at any time when the '$' is shown to finish and pay.
$21.54
tax?[yes or no]n

						Subtotal:	$21.54
						Tax Total:  $0
						Total:	$21.54
$31.99
tax?[yes or no]y

						Subtotal:	$53.53
						Tax Total:  $1.9194
						Total:	$76.9894
$0
how much will you pay?
$76.9894

						Total:	$76.9894
						Paid:	        $76.9894
						Change:	$7.62939e-06

I put in the paid was exactly the same as the total, and I get a number for change… I'm still debugging… Edit: now I added the language…


ghost's Avatar
0 0

What language are you programming in?


ynori7's Avatar
Future Emperor of Earth
0 0

I believe all you need to do is:

cout<<fixed<<your_number<<endl;```

EDIT: http://www.cplusplus.com/reference/iostream/manipulators/fixed/

ghost's Avatar
0 0

I know what truncating is, but I really don't think somthing like $4.6543 needs rounding. I just want two digits after the decimal point, and not just for output. here the new problem with truncating for output only:

						Subtotal:	     $66.42
						Tax Total:	$2.07
						Total:		$105.07

$0
how much will you pay?
$105.07

						Subtotal:	    $66.42
						Tax Total:	$2.07
						Total:		$0.00
how much will you pay?
$5

						Total:	$0.00
						Paid:	$110.07
						Change:	$5.00

Edit: I didn't know money was rounded… well, I have a solution anyway… I'll add .005 before I truncate, but there's still the problem of the program's inner math continuing the program…


ynori7's Avatar
Future Emperor of Earth
0 0

Zkunxen wrote: I need it to quit if the amount paid equals the total to within 2 decimal places because anything further should be cut off anyway.


if( fabs(total-paid) < .01 )
{ 
  quit 
}```

ghost's Avatar
0 0

I found an alternative, for now. I'll add .005 for every taxed item, then truncate it, and evaluate for less than 1 cent rather than equal to zero to quit the program. now I only have 2 logic bugs: for every 2 taxed items, even if they don't round up, 1 cent is added, and if the payment is exact, the change is $-0.00… I'll fix it later, I only made this to prove I could, not for commercial use.


ghost's Avatar
0 0

I've only quickly skimmed through this thread, as I'm just wasting a few spare minutes I have; but would it not be easier to store your 'money' as integers and have them as cents(cents is the equivalent to pence, right?)? Then just format them with to add the decimal in the right place before you output anything?


ghost's Avatar
0 0

x_5631 wrote: I've only quickly skimmed through this thread, as I'm just wasting a few spare minutes I have; but would it not be easier to store your 'money' as integers and have them as cents(cents is the equivalent to pence, right?)? Then just format them with to add the decimal in the right place before you output anything? Not really, but on the other hand, he could write a small piece of code to round the input he receives and stores so all values will only have 2 decimals at most. However, I'd suggest double instead of float for that one as float can net you some pretty freaky answers pretty quickly.


ghost's Avatar
0 0

I was considering using cents, too, but I did find a solution. the way I was calculating tax was wrong, but I fixed it and got it working. now I'm having another issue…


ghost's Avatar
0 0

nope. I felt like writing a cash register program to keep better track of my money. my schools programming course is visual basic, and I dropped out because I was the only one in the class that knew anything about computers. I spent the entire first 3 days of the class helping the teacher help everyone else set up their Macs to run windows on parallels and set up the visual basic program (both of which I've never used before…) and I hate microsoft. I have no idea why we couldn't have learned Java, and use the preinstalled Java compiler on the macs… I just like programming.


ghost's Avatar
0 0

MoshBat wrote: [quote]Zkunxen wrote: I hate microsoft.

You give me enough reasons behind that statement, and I'll let you walk out with your dignity.[/quote] how about being locked out of my account after changing my username, and the welcome screen still displayed the old name, requiring me to reinstall Vista totally because ophcrack did nothing more than freeze then crash, and having to reinstall debian twice after XP overwrote the bootsector, and the entire computer refused to boot after a fresh install of windows (that's not to mention the fact that my dell computer came with a graphics card with only an outdated driver available for it, and that my wireless card isn't supported by linux, so I had to cut off everyone elses wifi to install my OS again) And you need anti-virus software, whereas with pretty much any other operating system you don't have to worry about viruses as much. you also need to clear the registry occasionally or the computer can quickly become ungodly slow, even without viruses. not only that, but windows has all the games, and you get distracted and get nothing done. their languages aren't portable, and work only on windows. and if you sign up for their emails you get tons of spam, and if you've ever tried to design a web page you can't argue with the fact that you at least hate Microsoft's web browser. also, Vista's aero is completely pointless and eats up too much processor power, and always turns itself back on when you switch it off (especially so after a fresh install). all it does is make windows look better. they could have better invested their time in a utilitarian approach.

but it could also be that I've had bad experiences with windows and I'm just projecting…


ghost's Avatar
0 0

Hey popularity is a good reason to hate (most) windows users at least. I swear I get a call a week from friends co-workers etc, asking me to fix their crap. While this isnt a result of Microsoft, but rather a fallacy in Darwinism, Im going to choose when and where to place blame and hate.


fuser's Avatar
Member
0 1

MoshBat wrote: I too get a lot of fixing requests. Most don't take long. And quite often, I'll get something in return… A free laptop, for example.

Most of us don't get anything in return, I once asked a friend for some cash and then he goes on about this "bros over ho's" type of yarn, which is something since I didn't want the money on a date, just to get a magazine.

So these days I dread it if someone ask me to help them with their computer, since the majority of the problems can be solved easily.

Lock this thread. The OP's problem is solved, and if anyone wants to debate further on this, go to either the Computer General subsection or the Off-Topic subsection for debating the strengths and weaknesses of Windows.


ghost's Avatar
0 0

MoshBat wrote: CTRL+ALT+Del at login, else safe mode, Administrator account. Hurr-Durr.

not on vista, CTRL+ALT+Del at login does nothing, admin account is still disabled in safe mode

Another easily fixed "problem".

never said it wasn't, just annoying to have to go through the whole process again

Your fault for getting a Dell, nothing to do with Windows. I know that, I wasn't anticipating using linux when I bought it, I'm just pointing out that this makes it harder to reinstall with net install to avoid all those unnecessary packages Yes it is, you just don't know how. ya, it's called NDISwrapper, but when you use NDISwrapper, your wireless card doesn't support monitor mode for making sure my home wireless network is secure Wrong. If around 50% of people used Linux, viruses for Ubuntu would be made, leading to Anti-Virus software. Again, nothing to do with Windows as an OS, more it's popularity. this is true, but at the moment, linux and mac are safer than windows. thus it's good if people like it. There are alternatives, most people use Firefox, so that is irrelevant. Also, they're banned from shipping Windows with IE over here in Europe. true, but if you want to design a page that works for the people that use internet explorer, it's bad practice to make it say "Sorry, you can't see this page on internet explorer." and no IE on PCs in Europe? that's interesting… Next, your arguments are weak, almost as if you've been quickly looking through forums for various problems that old ladies have come across, and put them here. Have you used Windows, huh? You may have switched the thing on, moved that thing we call a "cursor", but you can't even solve basic problems. yes, I have used Windows, and every problem I mentioned is one I had to deal with, I didn't just look for problems. so what if I can't solve "Simple" problems, most of them are problems I caused anyway, but those same problems can't be recreated on any other OS that I've used.

besides, me hating windows is my opinion. All I said is I hate Microsoft. I didn't try to convice anyone that they should hate it, too. if you like it, that's totally up to you. me, I prefer my Mac.

Furthermore, you can get WINE, and play games there. Ouch, so Linux now distracts you, and stops you working. I could never get WINE to work right, but I hardly use Windows programs anyway

most of the programs I actually use work better on *nix based systems. and I know it's my fault for getting distracted, but because games are more easily available on windows, it's easier to set up and play them. thus, it's easier t get distracted. that argument was pointless, though. most of the work I do on my computer is just learning new things about C++, and I don't have to do that, it's not for school or anything. if I get distracted it doesn't make an ounce (or 28.3 grams, if you prefer) of difference.