Follow us on Twitter!
Capitalism is an Island of wealth in a sea of poverty
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 34
Guests Online: 25
Members Online: 9

Registered Members: 82878
Newest Member: defcon812
Latest Articles
View Thread

HellBound Hackers | HellBound Hackers | Questions

Author

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


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 02:07
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:
Code

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...

Edited by on 29-09-09 02:12
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with?


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 02:09
What language are you programming in?


Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 29-09-09 02:15
http://www.cplusp. . .precision/


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 02:30

if that's really the only way, then I'm using printf.
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 29-09-09 02:38
I believe all you need to do is:
Code
cout.precision(2);
cout<<fixed<<your_number<<endl;




EDIT: http://www.cplusp. . .ors/fixed/


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 29-09-09 02:40
ynori7 http://halls-of-valhalla.org
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 02:54
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:
Code

                  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...

Edited by on 29-09-09 03:00
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 29-09-09 03:06
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.


Code
#include<math>

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





halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 03:23
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.
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 10:22
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?


Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 11:33
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.


Author

RE: new problem


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 17:29
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...
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 29-09-09 18:12
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.
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-10-09 03:35
MoshBat wrote:
Zkunxen wrote:
I hate microsoft.

You give me enough reasons behind that statement, and I'll let you walk out with your dignity.

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...
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-10-09 04:55
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.


Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in

fuser
Member



Posts: 960
Location: in front of a computer (duh)
Joined: 05.04.07
Rank:
Mad User
Posted on 01-10-09 08:19
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.


img.userbarz.com/51/10006.png
img.userbarz.com/146/29144.gif
img.userbarz.com/99/19602.jpg
img.userbarz.com/4/600.png
img.userbarz.com/45/8814.gif
img360.imageshack.us/img360/9231/bfbarlr0.jpg
[url=http://userbarz.com/][img]ht
catinthecpu@hotmail.com
Author

RE: what's the best way to truncate a floating point number to 2 decimal places to work with in


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-10-09 18:05
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.