
-----------------------------------
xHoly-Divinity
Mon Oct 03, 2005 3:33 pm

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

-----------------------------------
Tony
Mon Oct 03, 2005 3:43 pm


-----------------------------------
rounding money? Go rent Office Space :lol:

Work in whole units (cents), the rest is just formatting.

-----------------------------------
xHoly-Divinity
Mon Oct 03, 2005 3:47 pm


-----------------------------------
There is no way just to round to two decimals??

-----------------------------------
Tony
Mon Oct 03, 2005 3:55 pm


-----------------------------------
multiply by 100, round, then divide back by 100

-----------------------------------
wtd
Mon Oct 03, 2005 3:57 pm


-----------------------------------
Don't keep track of money using floating point numbers.

-----------------------------------
Hikaru79
Mon Oct 03, 2005 4:54 pm


-----------------------------------
Don't keep track of money using floating point numbers.
Amen. If you don't know why he's saying that, just run this: 	public static void main(String 
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
Mon Oct 03, 2005 5:19 pm


-----------------------------------

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.

-----------------------------------
rizzix
Tue Oct 04, 2005 10:09 am


-----------------------------------
even better

System.out.printf("%.2f", someNumber); Or if you are going to save it as a string...

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.

-----------------------------------
beard0
Tue Oct 04, 2005 11:57 am


-----------------------------------
Don't keep track of money using floating point numbers.
Amen. If you don't know why he's saying that, just run this: 	public static void main(String 
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:
public static void main(String 

-----------------------------------
wtd
Tue Oct 04, 2005 12:06 pm


-----------------------------------
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
Tue Oct 04, 2005 12:24 pm


-----------------------------------
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
Tue Oct 04, 2005 12:39 pm


-----------------------------------
What is this?
That was Ruby. wtd was showing you that it's the same across different languages.

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
Tue Oct 04, 2005 1:07 pm


-----------------------------------
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.

-----------------------------------
Tony
Tue Oct 04, 2005 2:09 pm


-----------------------------------

Is this then a hardware issue?
Not quite, as rizzix pointed out

java's floating points strictly follow the IEEE 754 specs


 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.



put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false
put 10 + 0.1 + 0.01 = 0.01 + 0.1 + 10 %true


-----------------------------------
rizzix
Tue Oct 04, 2005 2:15 pm


-----------------------------------
No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently.

-----------------------------------
[Gandalf]
Tue Oct 04, 2005 3:47 pm


-----------------------------------
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?

-----------------------------------
beard0
Tue Oct 04, 2005 3:50 pm


-----------------------------------
"]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.

-----------------------------------
wtd
Tue Oct 04, 2005 4:39 pm


-----------------------------------
No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently.

Are you sure?  :)

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:~ $

-----------------------------------
rizzix
Tue Oct 04, 2005 9:13 pm


-----------------------------------
ooh. guess not.. i just typed in the number (didn't use any variables) seemed to have printed out True. Wow, so unpredictable.. hmm

-----------------------------------
wtd
Tue Oct 04, 2005 9:22 pm


-----------------------------------
$ 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 ]=>

-----------------------------------
beard0
Tue Oct 04, 2005 9:37 pm


-----------------------------------
Hooray for:x = 5.0;
y = 0.02;
z = 0.01;
If
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.

-----------------------------------
wtd
Fri Oct 07, 2005 5:24 pm


-----------------------------------
O'Caml:

# let x = 3.0 and y = 0.02 and z = 0.01 in 
   x +. y +. z = y +. x +. z;;
- : bool = true

-----------------------------------
wtd
Fri Oct 07, 2005 7:26 pm


-----------------------------------
SML/NJ:

- 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

-----------------------------------
xHoly-Divinity
Wed Nov 30, 2005 5:47 pm


-----------------------------------
I think that multiplying by 100, rounding, then dividing by 100 is the fastest way to do it...

-----------------------------------
MysticVegeta
Wed Nov 30, 2005 6:02 pm


-----------------------------------
Did you check the date on the topic?

-----------------------------------
xHoly-Divinity
Tue Jan 17, 2006 5:11 pm


-----------------------------------
Did you check the date on the topic?

Why?

-----------------------------------
beard0
Tue Jan 17, 2006 5:17 pm


-----------------------------------
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.
