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 Laughing

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

Author:  1of42 [ Mon Oct 03, 2005 5:19 pm ]
Post subject: 

Java:

DecimalFormat formatter = new DecimalFormat ("$0.00");
System.out.println(formatter.format(someNumber));


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

Java:
System.out.printf("%.2f", someNumber);
Or if you are going to save it as a string...

Java:
String s = String.format("%.2f", someNumber);



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:
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?!");
                };
}

Author:  wtd [ Tue Oct 04, 2005 12:06 pm ]
Post 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>

Author:  beard0 [ Tue Oct 04, 2005 12:24 pm ]
Post 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?

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:

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.

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.
PHP:
<?php
$x = 0.2;
$y = 0.01;
$z = 5.0;

echo $x + $y + $z . "\n";
echo $z + $y + $x . "\n";
if ($x + $y + $z == $z + $y + $x){
        echo "True!\n";
}else{
        echo "False\n";
}
if ($x + $y + $z === $z + $y + $x){
        echo "True!\n";
}else{
        echo "False\n";
}
?>


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.


Turing:

put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false
put 10 + 0.1 + 0.01 = 0.01 + 0.1 + 10 %true

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: 

code:
put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false

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:
code:
put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false

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? Smile

code:
insaneones@ubuntu:~ $ ghci
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.2.2, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Prelude> let (x, y, z) = (5.0, 0.01, 0.02)
Prelude> x + y + z == z + y + x
False
Prelude> :q
Leaving GHCi.
insaneones@ubuntu:~ $ hugs
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2003
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: hugs-bugs@haskell.org
||   || Version: November 2003  _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Type :? for help
Prelude> let (x, y, z) = (5.0, 0.01, 0.02) in x + y + z == z + y + x
False
Prelude> :q
[Leaving Hugs]
insaneones@ubuntu:~ $

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: 

code:
$ ledit mit-scheme
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright 2004 Massachusetts Institute of Technology.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Tuesday October 19, 2004 at 6:00:49 AM
  Release 7.7.90 || Microcode 14.11 || Runtime 15.3 || SF 4.41 || LIAR 4.116
  Edwin 3.114

1 ]=> (let ((x 5.0 )
            (y 0.02)
            (z 0.01))
         (if (= (+ x y z) (+ y z x))
            "foo"
            "bar"))
;Value 11: "bar"

1 ]=>

Author:  beard0 [ Tue Oct 04, 2005 9:37 pm ]
Post subject: 

Hooray for:
Mathematica:
x = 5.0;
y = 0.02;
z = 0.01;
If[x + y + z == y + z + x, "True", "False"]
(*This returns "True"!!!!*)

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:

code:
# let x = 3.0 and y = 0.02 and z = 0.01 in
   x +. y +. z = y +. x +. z;;
- : bool = true

Author:  wtd [ Fri Oct 07, 2005 7:26 pm ]
Post subject: 

SML/NJ:

code:
- open Real;
- let val x = 3.0
=     val y = 0.03
=     val z = 0.01
= in
=     Real.==(x + y + z, y + x + z)
= end;
val it = true : bool

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.


: