Computer Science Canada Quick question on rounding :-) |
Author: | xHoly-Divinity [ Mon Oct 03, 2005 3:33 pm ] |
Post subject: | Quick question on rounding :-) |
Quick question. How do you round to two decimal places. I need two decimal places because it is dealing with money. Thanks |
Author: | Tony [ Mon Oct 03, 2005 3:43 pm ] |
Post subject: | |
rounding money? Go rent Office Space Work in whole units (cents), the rest is just formatting. |
Author: | xHoly-Divinity [ Mon Oct 03, 2005 3:47 pm ] |
Post subject: | |
There is no way just to round to two decimals?? |
Author: | Tony [ Mon Oct 03, 2005 3:55 pm ] |
Post subject: | |
multiply by 100, round, then divide back by 100 |
Author: | wtd [ Mon Oct 03, 2005 3:57 pm ] |
Post subject: | |
Don't keep track of money using floating point numbers. |
Author: | Hikaru79 [ Mon Oct 03, 2005 4:54 pm ] | ||
Post subject: | |||
wtd wrote: Don't keep track of money using floating point numbers.
Amen. If you don't know why he's saying that, just run this:
It's just ridiculous that an enterprise-level language like Java can't do something as simple as add. wtd, if there's a technical reason behind this, could you explain it, 'cause I was just stunned when I first discovered this. |
Author: | 1of42 [ Mon Oct 03, 2005 5:19 pm ] | ||
Post subject: | |||
This will print out a formatted number for money - you don't need to round beforehand, it will take care of that stuff for you. |
Author: | rizzix [ Tue Oct 04, 2005 10:09 am ] | ||||
Post subject: | |||||
even better
Hikaru79: Java float's will behave the way you're expecting them to behave (for small numbers).. the doubles always behave likewise.. You have to round them to correct decimal place... Also.. if you need arbitrary precission.. then there's always the BigDecimal and BigInteger class. Besides java's floating points strictly follow the IEEE 754 specs.. i guess that's just the way the arithmetic is ...according to those specs. |
Author: | beard0 [ Tue Oct 04, 2005 11:57 am ] | ||||
Post subject: | |||||
Hikaru79 wrote: wtd wrote: Don't keep track of money using floating point numbers.
Amen. If you don't know why he's saying that, just run this:
It's just ridiculous that an enterprise-level language like Java can't do something as simple as add. wtd, if there's a technical reason behind this, could you explain it, 'cause I was just stunned when I first discovered this. Check this one out:
|
Author: | wtd [ Tue Oct 04, 2005 12:06 pm ] | ||
Post subject: | |||
|
Author: | beard0 [ Tue Oct 04, 2005 12:24 pm ] | ||
Post subject: | |||
wtd wrote:
What is this? |
Author: | Tony [ Tue Oct 04, 2005 12:39 pm ] | ||
Post subject: | |||
beard0 wrote: What is this?
That was Ruby. wtd was showing you that it's the same across different languages.
Turing too. |
Author: | beard0 [ Tue Oct 04, 2005 1:07 pm ] | ||
Post subject: | |||
Hmm, looks like Java is in some ways the best of the bunch then, as it actually outputs two different numbers, rather than outputing 5.03 both times, and then claiming that they are not equal. Damn, php too.
Is this then a hardware issue? I could see it comming from decimal terminating numbers being non-terminators in binary, if real numbers/doubles are held in memory in something like scientific notation, so that having the 5 added at the begining allows for less precision after the decimal place. |
Author: | Tony [ Tue Oct 04, 2005 2:09 pm ] | ||
Post subject: | |||
beard0 wrote: Is this then a hardware issue? Not quite, as rizzix pointed out rizzix wrote: java's floating points strictly follow the IEEE 754 specs IEEE 754 wrote: Why is 0.1 not 0.1? Binary floating-point numbers consist of signed integers multiplied by powers of two. When fractional, you can also consider them as integers divided by powers of two. The decimal number 0.1, or 1/10, is not an integer over a power of two. The 854 standard encompasses decimal arithmetic, but there is little hardware support outside of desktop calculators.
|
Author: | rizzix [ Tue Oct 04, 2005 2:15 pm ] |
Post subject: | |
No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently. |
Author: | [Gandalf] [ Tue Oct 04, 2005 3:47 pm ] | ||
Post subject: | |||
I don't get it... It IS false, since one of the numbers is 0.1, and the other is 0.01. So what's so special about this? |
Author: | beard0 [ Tue Oct 04, 2005 3:50 pm ] | ||
Post subject: | |||
[Gandalf] wrote:
I don't get it... It IS false, since one of the numbers is 0.1, and the other is 0.01. So what's so special about this? You know what he meant... or at least, if you read the rest of the topic you do. |
Author: | wtd [ Tue Oct 04, 2005 4:39 pm ] | ||
Post subject: | |||
rizzix wrote: No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently.
Are you sure?
|
Author: | rizzix [ Tue Oct 04, 2005 9:13 pm ] |
Post subject: | |
ooh. guess not.. i just typed in the number (didn't use any variables) seemed to have printed out True. Wow, so unpredictable.. hmm |
Author: | wtd [ Tue Oct 04, 2005 9:22 pm ] | ||
Post subject: | |||
|
Author: | beard0 [ Tue Oct 04, 2005 9:37 pm ] | ||
Post subject: | |||
Hooray for:
N.B. The absence of a ; on the last line is intentional. In mathematica, no end of line chacter is necessary, the semi-colon simply serves to supress output from the last statement. Had I placed a semi-colon on the end of the last line, I would have had no ouput. |
Author: | wtd [ Fri Oct 07, 2005 5:24 pm ] | ||
Post subject: | |||
O'Caml:
|
Author: | wtd [ Fri Oct 07, 2005 7:26 pm ] | ||
Post subject: | |||
SML/NJ:
|
Author: | xHoly-Divinity [ Wed Nov 30, 2005 5:47 pm ] |
Post subject: | |
I think that multiplying by 100, rounding, then dividing by 100 is the fastest way to do it... |
Author: | MysticVegeta [ Wed Nov 30, 2005 6:02 pm ] |
Post subject: | |
Did you check the date on the topic? |
Author: | xHoly-Divinity [ Tue Jan 17, 2006 5:11 pm ] |
Post subject: | |
MysticVegeta wrote: Did you check the date on the topic?
Why? |
Author: | beard0 [ Tue Jan 17, 2006 5:17 pm ] |
Post subject: | |
xHoly-Divinity wrote: MysticVegeta wrote: Did you check the date on the topic?
Why? FFS, because you're necro-posting, bringing up old topics - this is not appreciated, and you have just done it again. If the topic is old, let it die. Read it if you're interested, and if you feel an overwelming urge to share some revelation that a certain post has brought to you, PM the poster, and leave the old topic be. |