Posted: 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
Sponsor Sponsor
Tony
Posted: Mon Oct 03, 2005 3:43 pm Post subject: (No subject)
rounding money? Go rent Office Space
Work in whole units (cents), the rest is just formatting.
xHoly-Divinity
Posted: Mon Oct 03, 2005 3:47 pm Post subject: (No subject)
There is no way just to round to two decimals??
Tony
Posted: Mon Oct 03, 2005 3:55 pm Post subject: (No subject)
multiply by 100, round, then divide back by 100
wtd
Posted: Mon Oct 03, 2005 3:57 pm Post subject: (No subject)
Don't keep track of money using floating point numbers.
Hikaru79
Posted: Mon Oct 03, 2005 4:54 pm Post subject: (No 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:
Java:
public static void main(String [] args){
double x = 5.02;
double y = 0.01;
System.out.println(x+y);
}
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.
1of42
Posted: Mon Oct 03, 2005 5:19 pm Post subject: (No subject)
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.
Sponsor Sponsor
beard0
Posted: Tue Oct 04, 2005 11:57 am Post subject: (No 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:
Java:
public static void main(String [] args){
double x = 5.02;
double y = 0.01;
System.out.println(x+y);
}
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:
Java:
public static void main(String [] args){
double x = 0.02;
double y = 0.01;
double z = 5;
if (z+x+y == x+y+z){
System.out.println("Good");
}else{
System.out.println("What the hell?!");
};
}
wtd
Posted: Tue Oct 04, 2005 12:06 pm Post subject: (No subject)
code:
irb(main):001:0> x, y, z = 0.02, 0.01, 5.0
=> [0.02, 0.01, 5.0]
irb(main):002:0> z + x + y
=> 5.03
irb(main):003:0> x + y + z
=> 5.03
irb(main):004:0> z + x + y == x + y + z
=> false
irb(main):005:0>
beard0
Posted: Tue Oct 04, 2005 12:24 pm Post subject: (No subject)
wtd wrote:
code:
irb(main):001:0> x, y, z = 0.02, 0.01, 5.0
=> [0.02, 0.01, 5.0]
irb(main):002:0> z + x + y
=> 5.03
irb(main):003:0> x + y + z
=> 5.03
irb(main):004:0> z + x + y == x + y + z
=> false
irb(main):005:0>
What is this?
Tony
Posted: Tue Oct 04, 2005 12:39 pm Post subject: (No subject)
beard0 wrote:
What is this?
That was Ruby. wtd was showing you that it's the same across different languages.
Turing:
var x, y, z :real
x :=0.02
y :=0.01
z :=5.0
put x + y + z
put z + y + x
put x + y + z = z + y + x
Turing too.
beard0
Posted: Tue Oct 04, 2005 1:07 pm Post subject: (No 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.
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.
Tony
Posted: Tue Oct 04, 2005 2:09 pm Post subject: (No 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.